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> }

例如

 db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
"description": "must be a string and is required"
}
}
}
}
}
}
})

其它查询表达式

$near,$nearSphere,$text,$where

db.createCollection( "contacts",
{ validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )

行为

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. shell 边边角角

    [Shell学习笔记] 数组.关联数组和别名使用 Linux中bash脚本数组和字典使用举例 Linux Shell 通配符.元字符.转义符使用实例介绍

  2. Mac佳软之Understand---Android源码分析阅读神器

    下载地址, 密码:gebp 供大家体验, 请大家支持正版!!! https://www.jianshu.com/p/06f25d9131de

  3. Java基础系列 - 抽象类,子类继承

    package com.company; /** * 抽象类继承 * 用abstract修饰类就是抽象类 * 用abstract修饰方法就是抽象方法(一般使用比较少) * 抽象类不能被实例化 */ p ...

  4. 跑shell脚本出错问题以及解决。

    脚本信息: #!/bin/bash function hello(){ cat <<YJT echo "hello world!!!" YJT } hello 错误: ...

  5. docker笔记--docker 各系统安装

    在线安装 Docker 在 CentOS/RHEL 中安装 Docker 在终端中运行下面的命令安装 Docker. sudo yum install -y yum-utils sudo yum-co ...

  6. mybatis xml中是sql语句报错: Error creating document instance. Cause: org.xml.sax.SAXParseException: The

    最近项目折腾了老半天,找了资料才知道是这么回事... 因为语句中有一个小于号“<”,在XML中,会被当成一个页面元素来解析,不会处理为mysql的SQL语句的组成部分,修改如下: 1.在xml的 ...

  7. select选中

    比如<select class="selector"></select>    1.设置value为“全部“的项选中  复制代码代码如下:   $(&quo ...

  8. linux 的real time 、user time、 sys time

    <APUE>上提到了三个时间Real time, User time和Sys time.这三者是什么关系呢?在使用time(1)命令的时候,为什么real time < user t ...

  9. unittest 的用法

    一.discover方法 discover方法可以根据标准加载用例,并将结果返回给测试套件(suite),start_dir:待测试的目录,pattern:测试用例文件名的匹配规. 如: start_ ...

  10. python 设计模式之访问者模式

    写在前面 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情形下实现 ...