如何强制对 MongoDB 中的文档值进行类型检查? [英] How to enforce type check for a value of document in MongoDB?

查看:55
本文介绍了如何强制对 MongoDB 中的文档值进行类型检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RDBMS 中,每列都有类似 CHARINTEGER 等的类型.

In RDBMS each columns has type like CHAR, INTEGER etc..

在 MongoDB 中,使用 mongo shell api 在 people 集合中添加以下文档:

In MongoDB, adding below document in people collection, using mongo shell api:

 > db.people.insert({"name": 1})

MongoDB 允许为 name" 键添加值 1,这是不正确的

MongoDB allows adding value 1 for "name" key, which is incorrect

如何在集合中添加文档之前强制执行类型检查?

How to enforce type check before adding a document in a collection?

推荐答案

你可以这样做.这只是一个例子.

You can do it like this. This just an example.

db.createCollection( "people" , {
   validator: {
     $jsonSchema: {
        bsonType: "object",
        additionalProperties: false, //wont't allow additional properties to be added, Use if you want to restrict people from adding extra fields.
 required: ["name","age"], //document must contain these fields, else operation will fail/log warning depending on `validationLevel` and `validationAction`
        properties: {
           _id : {
              bsonType: "objectId" },
           name: {
              bsonType: "string", //type of name
              description: "required and must be a string" },
           age: {
              bsonType: "int", //type of age
              minimum: 0,
              maximum: 100,
              description: "required and must be in the range 0-100" }
        }
     }},
     validationLevel: "moderate",
     validationAction: "error"
})

  • 要在创建新集合时指定验证规则,请将 db.createCollection()validator 选项一起使用.然后在文档 validator 中使用 $jsonSchema 以在 insertupdate 操作上强制执行指定的模式.

    • To specify validation rules when creating a new collection, use db.createCollection() with the validator option. Then use $jsonSchema in a document validator to enforce the specified schema on insert and update operations.

      validationLevel 选项,它确定 MongoDB 在更新期间对现有文档应用验证规则的严格程度,以及

      validationLevel option, which determines how strictly MongoDB applies validation rules to existing documents during an update, and

      validationAction 选项,用于确定 MongoDB 是否应出错并拒绝违反验证规则的文档,或警告日志中的违规但允许无效文档.

      validationAction option, which determines whether MongoDB should error and reject documents that violate the validation rules or warn about the violations in the log but allow invalid documents.

      要将文档验证添加到现有集合,请使用带有 validator 选项的 collMod 命令.

      To add document validation to an existing collection, use collMod command with the validator option.

      当您向集合添加验证时,现有文档在修改之前不会进行验证检查.

      When you add validation to a collection, existing documents do not undergo validation checks until modification.

      请通读这些链接:-

      https://docs.mongodb.com/manual/core/schema-验证/

      https://docs.mongodb.com/手册/参考/操作员/查询/jsonSchema/#op._S_jsonSchema

      这篇关于如何强制对 MongoDB 中的文档值进行类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆