validateDBMetadata

On this page本页内容

Definition定义

New in version 5.0.在版本5.0中新增

validateDBMetadata

The validateDBMetadata command checks that the stored metadata of a database or a collection is valid within a particular API version.validateDBMetadata命令检查数据库或集合的存储元数据在特定API版本中是否有效。

validateDBMetadata reports errors, but does not have the capability to fix errors.报告错误,但无法修复错误。

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand( {
   validateDBMetadata: 1,
   apiParameters: {
      version: <string>,
      strict: <boolean>,
      deprecationErrors: <boolean>
   },
   db: <string>,
   collection: <string>,
} )

Command Fields

The command takes the following fields:该命令接受以下字段:

Field字段Type类型Description描述
apiParametersdocument

All Fields are Required.所有字段均为必填字段。

  • version (string)

    The API Version to validate against. 要验证的API版本。For now, "1" is the only version.目前,"1"是唯一的版本。

  • strict (boolean)

    If true, APIStrictError responses will be included in the output.如果为true,则输出中将包含APIStrictError响应。

  • deprecationErrors (boolean)

    If true, APIDeprecationError responses will be included in the output.如果为true,则输出中将包含APIDeprecationError响应。

dbstringOptional. 可选。The name of the database to validate. 要验证的数据库的名称。If no database is specified, all databases will be validated. 如果未指定数据库,将验证所有数据库。
collectionstringOptional. 可选。The name of the collection or view to validate. 要验证的集合或视图的名称。If no collection or view is specified, all collections in the database specified by db will be validated. 如果未指定集合或视图,则将验证db指定的数据库中的所有集合。If no database is specified, all collections in all databases will be validated. 如果未指定数据库,将验证所有数据库中的所有集合。

Behavior行为

  • Validate all collections in all databases, reporting APIStrictError and APIVersionError error responses.验证所有数据库中的所有集合,报告APIStrictErrorAPIVersionError错误响应。

    db.runCommand( {
      validateDBMetadata: 1,
      apiParameters: {
        version: "1",
        strict: true,
        deprecationErrors: true
      },
    })
  • Validate all collections in inventory:验证inventory中的所有集合:

    db.runCommand( {
      validateDBMetadata: 1,
      apiParameters: {
        version: "1",
        strict: true,
        deprecationErrors: true
      },
      db: "inventory",
    })
  • Validate the sales collection in the inventory database:验证inventory数据库中的sales集合:

    db.runCommand( {
      validateDBMetadata: 1,
      apiParameters: {
        version: "1",
        strict: true,
        deprecationErrors: true
      },
      db: "inventory",
      collection: "sales",
    })
  • Validate any and all sales collections across all databases:验证所有数据库中的任何和所有sales集合:

    db.runCommand( {
      validateDBMetadata: 1,
      apiParameters: {
        version: "1",
        strict: true,
        deprecationErrors: true
      },
      collection: "sales",
    })
Note注意

Your user must have the validate privilege action on all collections you want to validate.您的用户必须对您要验证的所有集合具有validate权限操作。

Output输出

{
   apiVersionErrors: [
     {
       ns: <string>,
       code: <int>,
       codeName: <string>,
       errmsg: <string>
     }
   ],
   ok: <int>,
   hasMoreErrors: <boolean>,
}
validateDBMetadata.apiVersionErrors

Array of documents describing API Version errors.描述API版本错误的文档数组。

validateDBMetadata.apiVersionErrors[n].ns

Namespace of the collection or view with error.集合或视图的命名空间有错误。

validateDBMetadata.apiVersionErrors[n].code

Numeric error code.数字错误代码。

validateDBMetadata.apiVersionErrors[n].codeName

Name of the error code.错误代码的名称。

validateDBMetadata.apiVersionErrors[n].errmsg

String describing the error.描述错误的字符串。

validateDBMetadata.ok

If the command fails, ok is set to 1. Otherwise, ok is set to 0. 如果命令失败,ok设置为1。否则,ok设置为0validateDBMetadata.ok may have a value of 0 and still report validation errors.值可能为0,但仍报告验证错误。

validateDBMetadata.hasMoreErrors

If true, there are additional errors.如果为true,则存在其他错误。

Example示例

Use the sample Query API code to create a sales collection in mongosh:使用示例查询API代码在mongosh中创建sales集合:

db.sales.insertMany([
    { "_id" : 1, "item" : "shoes", "price" : 10, "quantity" : 2, "date" : ISODate("2021-01-01T08:00:00Z") },
    { "_id" : 2, "item" : "hat", "price" : 20, "quantity" : 1, "date" : ISODate("2021-02-03T09:00:00Z") },
    { "_id" : 3, "item" : "gloves", "price" : 5, "quantity" : 5, "date" : ISODate("2021-02-03T09:05:00Z") },
    { "_id" : 4, "item" : "pants", "price" : 10, "quantity" : 10, "date" : ISODate("2021-02-15T08:00:00Z") },
    { "_id" : 5, "item" : "socks", "price" : 5, "quantity" : 10, "date" : ISODate("2021-02-15T09:05:00Z") },
    { "_id" : 6, "item" : "shirt", "price" : 5, "quantity" : 5, "date" : ISODate("2021-02-15T12:05:10Z") },
    { "_id" : 7, "item" : "belt", "price" : 5, "quantity" : 10, "date" : ISODate("2021-02-15T14:12:12Z") },
    { "_id" : 8, "item" : "blouse", "price" : 10, "quantity" : 5, "date" : ISODate("2021-03-16T20:20:13Z") }
])

Add a text index on the item field.item字段上添加文本索引

db.sales.createIndex( { item: "text" } )

Validate the sales collection for strict compliance with API version 1 and include deprecationErrors in the output.验证sales集合是否严格符合API版本1,并在输出中包含depositionErrors

db.runCommand( {
  validateDBMetadata: 1,
  apiParameters: {
    version: "1",
    strict: true,
    deprecationErrors: true
  },
  collection: "sales",
})

validateDBMetadata reports an APIStrictError on the item_text index.validateDBMetadata报告item_text索引上的APIStrictError

{
   apiVersionErrors: [
     {
       ns: 'test.sales',
       code: 323,
       codeName: 'APIStrictError',
       errmsg: 'The index with name item_text is not allowed in API version 1.'
     }
   ],
   ok: 1,
   hasMoreErrors: false,
}
←  validatewhatsmyuri →