MongoDB -- 3.2新功能Document Validation

Document Validation

MongoDB provides the capability to validate documents during updates and insertions. Validation rules are specified on a per-collection basis using the validator option, which takes a document that specifies the validation rules or expressions. Specify the expressions using any query operators, with the exception of $geoNear, near,$nearSphere, text, and $where.

Add document validation to an existing collection using the collMod command with the validator option. You can also specify document validation rules when creating a new collection using db.createCollection() with the validator option, as in the following:

下面语句,执行建表,并且通过validator为表增加规则。

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

$or:至少满足一个表达式(http://docs.mongoing.com/manual-zh/reference/operator/query/or.html
表达式内容:

      [
         { phone: { $type: "string" } },
         { email: { $regex: /@mongodb\.com$/ } },
         { status: { $in: [ "Unknown", "Incomplete" ] } }
      ]

总结:
最终我们了解到,在创建文档时,我们就指定文档中特殊列的规则,对插入和更新操作进行控制。

如下图操作:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值