定义
版本 5.0 中的新增功能。
validateDBMetadataThe
validateDBMetadatacommand 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.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { validateDBMetadata: 1, apiParameters: { version: <string>, strict: <boolean>, deprecationErrors: <boolean> }, db: <string>, collection: <string>, } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
文档 | 所有字段均为必填字段。
| |
| 字符串 | 可选。 要验证的数据库名称。 如果未指定数据库,则将验证所有数据库。 |
| 字符串 | 可选。 要验证的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.okIf the command fails,
okis set to1. Otherwise,okis set to0.validateDBMetadata.okmay have a value of0and still report validation errors.
例子
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, }