查看现有验证规则

您可以查看集合的验证规则以确定对文档施加了哪些限制以及 MongoDB 在出现无效文档时如何处理它们。

要查看集合的验证规则,请使用 db.getCollectionInfos()方法或listCollections 数据库命令。

这两个命令返回相同的信息,但每个命令的输出格式不同。

先决条件

要运行此页面上的示例,请创建一个students包含验证规则的集合。有关详细信息,请参阅 指定 JSON 架构验证。

示例:db.getCollectionInfos()语法

以下命令用于db.getCollectionInfos()返回students集合的验证规则:

db.getCollectionInfos( { name: "students" } )[0].options.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'
      },
      gpa: {
        bsonType: [ 'double' ],
        description: 'must be a double if the field exists'
      }
    }
  }
}

[NOTE]

默认情况下不包括验证操作和级别

如果validationActionvalidationLevel未明确设置,db.getCollectionInfos()则不在其输出中包含这些字段。

示例listCollections语法

以下命令用于listCollections返回students集合的验证规则:

db.runCommand ( { listCollections: 1, filter: { name: "students" } } )

输出类似于以下对象:

{
  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
}

学到更多

参见

原文:View Existing Validation Rules

译者:景圣

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""