View Existing Validation Rules查看现有验证规则
On this page本页内容
You can view a collection's validation rules to determine what restrictions are imposed on documents and how MongoDB handles invalid documents when they occur.您可以查看集合的验证规则,以确定对文档施加了哪些限制,以及MongoDB在出现无效文档时如何处理这些限制。
To view a collection's validation rules, use the 若要查看集合的验证规则,请使用db.getCollectionInfos() method or listCollections database command.db.getCollectionInfos()方法或listCollections数据库命令。
Both commands return the same information, but the output format differs between each command.两个命令都返回相同的信息,但每个命令的输出格式不同。
Prerequisite先决条件
To run the examples on this page, create a 若要运行此页面上的示例,请创建一个具有验证规则的students collection with validation rules. students集合。For more information, see Specify JSON Schema Validation.有关更多信息,请参阅指定JSON架构验证。
Example: db.getCollectionInfos() Syntax示例:db.getCollectionInfos()语法
db.getCollectionInfos() SyntaxThe following command uses 以下命令使用db.getCollectionInfos() to return the validation rules for the students collection:db.getCollectionInfos()返回students集合的验证规则:
db.getCollectionInfos( { name: "students" } )[0].options.validator
The output resembles the following validation object:输出类似于以下验证对象:
{
  '$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'
      },
      gpa: {
        bsonType: [ 'double' ],
        description: 'must be a double if the field exists'
      }
    }
  }
}
Validation Action and Level Not Included by Default默认情况下不包括验证操作和级别
If 如果未显式设置validationAction and validationLevel are not explicitly set, db.getCollectionInfos() does not include those fields in its output.validationAction和validationLevel,则db.getCollectionInfos()的输出中不包括这些字段。
Example: 示例:listCollections Syntax语法
The following command uses 以下命令使用listCollections to return the validation rules for the students collection:listCollections返回students集合的验证规则:
db.runCommand ( { listCollections: 1, filter: { name: "students" } } )
The output resembles the following object:输出类似于以下对象:
{
  cursor: {
    id: Long("0"),
    ns: 'test.$cmd.listCollections',
    firstBatch: [
      {
        name: 'students',
        type: 'collection',
        options: {
          validator: {
            '$jsonSchema': {
              bsonType: 'object',
              required: [ 'name', 'year', 'major', 'address' ],
              properties: {
                name: {
                  bsonType: 'string',
                  description: 'must be a string and is required'
                },
                gpa: {
                  bsonType: [ 'double' ],
                  description: 'must be a double if the field exists'
                }
              }
            },
            validationAction: 'warn'
          }
        },
        info: {
          readOnly: false,
          uuid: UUID("bf560865-5879-4ec1-b389-f77a03abbc5a")
        },
        idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
      }
    ]
  },
  ok: 1
}