Definition定义
New in version 5.0.在版本5.0中新增。
validateDBMetadataThevalidateDBMetadatacommand checks that the stored metadata of a database or a collection is valid within a particular API version.validateDBMetadata命令检查数据库或集合的存储元数据在特定的API版本中是否有效。validateDBMetadatareports errors, but does not have the capability to fix errors.报告错误,但没有修复错误的能力。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
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:该命令包含以下字段:
apiParameters |
| |
db | ||
collection | db will be validated. 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.验证所有数据库中的所有集合,报告APIStrictError和APIVersionError错误响应。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验证salescollection in theinventorydatabase:inventory数据库中的sales集合:db.runCommand( {
validateDBMetadata: 1,
apiParameters: {
version: "1",
strict: true,
deprecationErrors: true
},
db: "inventory",
collection: "sales",
})Validate any and all验证所有数据库中的任何和所有salescollections across all databases:sales集合:db.runCommand( {
validateDBMetadata: 1,
apiParameters: {
version: "1",
strict: true,
deprecationErrors: true
},
collection: "sales",
})
Output输出
{
apiVersionErrors: [
{
ns: <string>,
code: <int>,
codeName: <string>,
errmsg: <string>
}
],
ok: <int>,
hasMoreErrors: <boolean>,
}
validateDBMetadata.apiVersionErrorsArray of documents describing API Version errors.描述API版本错误的文档数组。
validateDBMetadata.apiVersionErrors[n].nsNamespace of the collection or view with error.集合或视图的命名空间出错。
validateDBMetadata.apiVersionErrors[n].codeNumeric error code.数字错误代码。
validateDBMetadata.apiVersionErrors[n].codeNameName of the error code.错误代码的名称。
validateDBMetadata.apiVersionErrors[n].errmsgString describing the error.描述错误的字符串。
validateDBMetadata.okIf the command fails,如果命令失败,okis set to1. Otherwise,okis set to0.validateDBMetadata.okmay have a value of0and still report validation errors.ok设置为1。否则,ok设置为0。validateDBMetadata.ok的值可能为0,但仍会报告验证错误。
validateDBMetadata.hasMoreErrorsIf如果为true, there are additional errors.true,则存在其他错误。
Example示例
Use the sample Query API code to create a 使用示例Query API代码在sales collection in mongosh: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,并在输出中包含deprecationErrors。
db.runCommand( {
validateDBMetadata: 1,
apiParameters: {
version: "1",
strict: true,
deprecationErrors: true
},
collection: "sales",
})
validateDBMetadata reports an 在APIStrictError on the item_text index.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,
}