Mongo Datamodel Validation

mongo insert,update document时候的校验规则

指定validation rules

  • new collection

    db.createCollection(...,{validator:})
  • existing collection

    collMod command

json schema (version >=3.6)

$jsonSchema匹配满足指定的JSON Schema

语法

{ $jsonSchema: <JSON Schema object> }

例如

  1. db.createCollection("students", {
  2. validator: {
  3. $jsonSchema: {
  4. bsonType: "object",
  5. required: [ "name", "year", "major", "address" ],
  6. properties: {
  7. name: {
  8. bsonType: "string",
  9. description: "must be a string and is required"
  10. },
  11. year: {
  12. bsonType: "int",
  13. minimum: 2017,
  14. maximum: 3017,
  15. description: "must be an integer in [ 2017, 3017 ] and is required"
  16. },
  17. major: {
  18. enum: [ "Math", "English", "Computer Science", "History", null ],
  19. description: "can only be one of the enum values and is required"
  20. },
  21. gpa: {
  22. bsonType: [ "double" ],
  23. description: "must be a double if the field exists"
  24. },
  25. address: {
  26. bsonType: "object",
  27. required: [ "city" ],
  28. properties: {
  29. street: {
  30. bsonType: "string",
  31. description: "must be a string if the field exists"
  32. },
  33. city: {
  34. bsonType: "string",
  35. "description": "must be a string and is required"
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. })

其它查询表达式

$near,$nearSphere,$text,$where

  1. db.createCollection( "contacts",
  2. { validator: { $or:
  3. [
  4. { phone: { $type: "string" } },
  5. { email: { $regex: /@mongodb\.com$/ } },
  6. { status: { $in: [ "Unknown", "Incomplete" ] } }
  7. ]
  8. }
  9. } )

行为

validation 在insert,update 触发,collection新添加 validation,之前的document不会触发校验

validationlevel

validationlevel用来决定对于校验,mongo采用什么样的操作

  • strict(default)

应用校验到所有insert,update

  • moderate

应用校验到满足校验的document

  • off

关闭校验

validationaction

validationaction是mongo用来决定在没有通过校验时做什么操作

  • error (default)

拒绝所有违反校验的insert,update

  • warn

mongo会记录下来但会让其通过校验

不能在admin,local,config库和system.*集合中设置校验

Mongo Document 校验的更多相关文章

  1. Mongo 整体架构介绍(1)-------分片集群

    摘要 在mongo初识文中介绍了mongo与cassandra的主要区别,以及mongo物理部署架构图.本文接着上一篇的mongo 架构图,来继续讲分片集群. 分片介绍 shard key mongo ...

  2. MongoDB学习笔记~ObjectId主键的设计

    回到目录 说一些关于ObjectId的事 MongoDB确实是最像关系型数据库的NoSQL,这在它主键设计上可以体现的出来,它并没有采用自动增长主键,因为在分布式服务器之间做数据同步很麻烦,而是采用了 ...

  3. kettle之mongodb数据同步

    需求: 1.源数据库新增一条记录,目标库同时新增一条记录: 2.源数据库修改一条记录,目标库同时修改该条记录: 示例用到三个Kettle组件 下面详细说下每个组件的配置 Source: 本示例连接的是 ...

  4. mongodb 创建LBS位置索引

    <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver< ...

  5. kettle mogodb output详解

    以下主要来自官网文档,原文:https://wiki.pentaho.com/display/EAI/MongoDB+Output Configure Connection Tab 1 Host na ...

  6. 【实战】使用 Kettle 工具将 mysql 数据增量导入到 MongoDB 中

    最近有一个将 mysql 数据导入到 MongoDB 中的需求,打算使用 Kettle 工具实现.本文章记录了数据导入从0到1的过程,最终实现了每秒钟快速导入约 1200 条数据.一起来看吧~ 一.K ...

  7. Mongo = get size of single document

      Object.bsonsize(db.test.findOne({type:"auto"}))

  8. linux上启动tomcat报错:Failed to read schema document 'http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd

    本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! spring在加载xsd文件时总是先试图在本地查找xs ...

  9. WebFlux04 SpringBootWebFlux集成MongoDB之Windows版本、WebFlux实现CRUD、WebFlux实现JPA、参数校验

    1 下载并安装MongoDB 1.1 MongoDB官网 1.2 下载 solutions -> download center 1.3 安装 双击进入安装即可 1.3.1 安装时常见bug01 ...

随机推荐

  1. windows7上启动jmeter报错,寻求解决办法?

    背景: 已安装jdk 12,已配置环境变量,点击jmeter.bat 或者进入cmd启动jmter都无法启动 如图: 情况1.在cmd模式下报错 情况2: 打开运行,输入“powershell ise ...

  2. es 修改 mapping 字段类型

    一.原索引 PUT my_index { "mappings": { "_doc": { "properties": { "cre ...

  3. Cubic-bezier 曲线

    cubic-bezier又称三次贝塞尔,主要是为animation生成速度曲线函数. cubic-bezier(x1,y1,x2,y2) 此图中: P0:(0,0) P1:(x1,y1) P2:(x2 ...

  4. ElasticSearch及其插件安装配置

    elasticsearch安装使用 .安装步骤: 1.下载elasticsearch的rpm包: wget https://artifacts.elastic.co/downloads/elastic ...

  5. mysql 触发器语法详解

    1.创建Mysql触发器: 语法: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW BE ...

  6. viewbag

    How does ViewBag in ASP.NET MVC work behind the scenes? https://stackoverflow.com/a/16950197/3782855 ...

  7. presto计算日期间隔天数或者小时间隔——date_diff函数使用

    “Presto是Facebook最新研发的数据查询引擎,可对250PB以上的数据进行快速地交互式分析.据称该引擎的性能是 Hive 的 10 倍以上.”,亲身用过之后,觉得比hive快了10倍不止. ...

  8. OpenJudge计算概论-忽略大小写比较字符串大小

    /*======================================================================= 忽略大小写比较字符串大小 总时间限制: 1000ms ...

  9. xss绕过姿势

    #未完待续... 00x1.绕过 magic_quotes_gpc magic_quotes_gpc=ON 是php中的安全设置,开启后会把一些特殊字符进行轮换, 比如: ' 会被转换为 \' 再比如 ...

  10. SQL语言Select经典语句

    -- 示例数据 Select * From Employee Select * From Department -- 返回工资最高的员工的信息 Select * From Employee where ...