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. SLAM第一篇:基础知识

    无论在室内.野外.空中还是水下,SLAM是机器人进入未知环境遇到的第一个问题.本期给大家介绍SLAM的基础知识:传感器与视觉SLAM框架 近来年,智能机器人技术在世界范围内得到了大力发展.人们致力于把 ...

  2. [线性代数] 线性代数入门A Gentle Introduction

    An Overview: System of Linear Equations Basically, linear algebra solves system of linear equations ...

  3. CRF 初步了解

    国外有一个很著名的条件随机场的教程,是英文的,原文: http://blog.echen.me/2012/01/03/introduction-to-conditional-random-fields ...

  4. wmi 远程访问问题解决

    WMI远程访问问题解决方法 WMI 全称为:Microsoft Windows Management Instrumentation (WMI)  按微软的介绍大致如下:      WMI 是 Mic ...

  5. LoadRunner11下载、安装与破解

    目前LoadRunner最新版本已经更新到LR12了,但是只有试用版本,最多只支持50人的并发,所以我们还是只能用LR11的破解版,毕竟商用 版是真的很贵,一般企业怕是都不会去购买. 1.LoadRu ...

  6. 分析CSS布局神器

    只要在页面下加入如下css,整个css布局就清晰了 * { background-color: rgba(255,0,0,.2); } * * { background-color: rgba(0,2 ...

  7. Java高并发程序设计

    一.并行世界 摩尔定律的失效,2004年秋季,Intel宣布彻底取消4GHz计划,CPU向多核心的发展,顶级计算机科学家唐纳德丶尔文丶克努斯评价:在我看来,这种现象(并发)或多或少是由于硬件设计者已经 ...

  8. List根据多个字段分组

    List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen( ...

  9. web手工项目02-注册功能输入分析,处理,输出方法-测试用例及缺陷编写-首页轮播图和购物车

    web手工项目第二天笔记 昨日回顾 搭建测试环境(WAMP,phpStudy,tpshop项目文件) 熟悉项目(四个步骤,三个来源) 项目测试流程(需求评审,测试计划与方案,测试用例设计与评审,测试执 ...

  10. 123457---脑筋急转弯01--com.threeObj03.JiZhuanWan

    脑筋急转弯01--com.threeObj03.JiZhuanWan