对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

validateDBMetadata(数据库命令)

版本 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 报告错误,但没有能力修复错误。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

该命令具有以下语法:

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

该命令接受以下字段:

字段
类型
说明

文档

所有字段均为必填字段。

  • version (字符串)

    要验证的 API 版本。 目前, "1"是唯一的版本。

  • strict (布尔值)

    如果true , APIStrictError响应将包含在输出中。

  • deprecationErrors (布尔值)

    如果true ,则 APIDeprecationError响应将包含在输出中。

db

字符串

可选。 要验证的数据库名称。 如果未指定数据库,则将验证所有数据库。

collection

字符串

可选。 要验证的collection或视图的名称。如果未指定collection或视图,则将验证由db指定的所有collection。如果未指定数据库,则将验证所有数据库中的所有collection。

  • 验证所有数据库中的所有collection,报告APIStrictError和APIVersionError错误响应。

    db.runCommand( {
    validateDBMetadata: 1,
    apiParameters: {
    version: "1",
    strict: true,
    deprecationErrors: true
    },
    })
  • 验证inventory中的所有collection:

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

    db.runCommand( {
    validateDBMetadata: 1,
    apiParameters: {
    version: "1",
    strict: true,
    deprecationErrors: true
    },
    db: "inventory",
    collection: "sales",
    })
  • 验证所有数据库中的所有salescollection:

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

注意

您的用户必须对要验证的所有collection具有validate权限操作。

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

描述 API 版本错误的文档数组。

validateDBMetadata.apiVersionErrors[n].ns

出现错误的collection或视图的命名空间。

validateDBMetadata.apiVersionErrors[n].code

数字错误代码。

validateDBMetadata.apiVersionErrors[n].codeName

错误代码的名称。

validateDBMetadata.apiVersionErrors[n].errmsg

描述错误的字符串。

validateDBMetadata.ok

If the command fails, ok is set to 1. Otherwise, ok is set to 0. validateDBMetadata.ok may have a value of 0 and still report validation errors.

validateDBMetadata.hasMoreErrors

如果为true ,则存在其他错误。

Use the sample Query API code to create a sales collection in mongosh:

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") }
])

在item字段上添加文本索引。

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

验证sales集合是否严格符合 API 版本 1,并在输出中包含deprecationErrors 。

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

validateDBMetadata reports an APIStrictError on the item_text index.

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