当前位置:   article > 正文

MongoDB -- 3.2新功能Document Validation_mongodb validatedocuments:

mongodb validatedocuments:

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" ] } }
      ]
   }
} )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

      [
         { phone: { $type: "string" } },
         { email: { $regex: /@mongodb\.com$/ } },
         { status: { $in: [ "Unknown", "Incomplete" ] } }
      ]
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/286937
推荐阅读
相关标签
  

闽ICP备14008679号