Docs Home → Develop Applications → MongoDB Manual
On this page本页内容
If you add validation to your collection after you create it, or modify an existing validation schema, you may have invalid documents in your collection. 如果在创建集合后向其添加验证,或修改现有验证架构,则集合中可能有无效文档。Similarly, if your schema's 类似地,如果您的架构的validationAction
is warn
, your collection is allowed to contain invalid documents. validationAction
为warn
,则允许您的集合包含无效文档。You can query for invalid documents to potentially update or delete them from your collection.您可以查询无效文档,以可能从集合中更新或删除它们。
To find documents that either match or don't match a specified schema, use 要查找与指定模式匹配或不匹配的文档,请将$jsonSchema
with query operators. $jsonSchema
与查询运算符一起使用。Similarly, you can update or delete documents based on a schema by using 类似地,通过在写操作的查询条件中使用$jsonSchema
in query conditions for write operations.$jsonSchema
,可以基于模式更新或删除文档。
Create a sample collection 使用以下文档创建样本集合inventory
with the following documents:inventory
:
db.inventory.insertMany( [ { item: "journal", qty: NumberInt(25), size: { h: 14, w: 21, uom: "cm" }, instock: true }, { item: "notebook", qty: NumberInt(50), size: { h: 8.5, w: 11, uom: "in" }, instock: true }, { item: "paper", qty: NumberInt(100), size: { h: 8.5, w: 11, uom: "in" }, instock: 1 }, { item: "planner", qty: NumberInt(75), size: { h: 22.85, w: 30, uom: "cm" }, instock: 1 }, { item: "postcard", qty: NumberInt(45), size: { h: 10, w: 15.25, uom: "cm" }, instock: true }, { item: "apple", qty: NumberInt(45), status: "A", instock: true }, { item: "pears", qty: NumberInt(50), status: "A", instock: true } ] )
Define a sample schema object and store it in a variable called 定义一个示例模式对象并将其存储在名为myschema
:myschema
的变量中:
let myschema = { $jsonSchema: { required: [ "item", "qty", "instock" ], properties: { item: { bsonType: "string" }, qty: { bsonType: "int" }, size: { bsonType: "object", required: [ "uom" ], properties: { uom: { bsonType: "string" }, h: { bsonType: "double" }, w: { bsonType: "double" } } }, instock: { bsonType: "bool" } } } }
These commands return all documents that match the schema:这些命令返回与架构匹配的所有文档:
db.inventory.find(myschema) db.inventory.aggregate( [ { $match: myschema } ] )
Both commands return the same result:两个命令都返回相同的结果:
[ { _id: ObjectId("62b5cd5a14b92d148400f7a3"), item: 'apple', qty: 45, status: 'A', instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a4"), item: 'pears', qty: 50, status: 'A', instock: true } ]
To find all documents do not satisfy the schema:, use 要查找所有不满足schema:的文档,请使用$jsonSchema
with the $nor
operator:$jsonSchema
和$nor
运算符:
db.inventory.find( { $nor: [ myschema ] } )
Output:输出:
[ { _id: ObjectId("62b5cd5a14b92d148400f79e"), item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f79f"), item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a0"), item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, instock: 1 }, { _id: ObjectId("62b5cd5a14b92d148400f7a1"), item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, instock: 1 }, { _id: ObjectId("62b5cd5a14b92d148400f7a2"), item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, instock: true } ]
This command updates all documents that don't match the schema and sets the documents' 此命令更新所有与架构不匹配的文档,并将文档的isValid
field to false
:isValid
字段设置为false
:
db.inventory.updateMany( { $nor: [ myschema ] }, { $set: { isValid: false } } )
To verify the update, query the collection:要验证更新,请查询集合:
db.inventory.find()
Output:输出:
[ { _id: ObjectId("62b5cd5a14b92d148400f79e"), item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, instock: true, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f79f"), item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, instock: true, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f7a0"), item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, instock: 1, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f7a1"), item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, instock: 1, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f7a2"), item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, instock: true, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f7a3"), item: 'apple', qty: 45, status: 'A', instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a4"), item: 'pears', qty: 50, status: 'A', instock: true } ]
This command deletes all documents that don't match the schema:此命令删除与架构不匹配的所有文档:
db.inventory.deleteMany( { $nor: [ myschema ] } )
To verify the update, query the collection:要验证更新,请查询集合:
db.inventory.find()
Output:输出:
[ { _id: ObjectId("62b5cd5a14b92d148400f7a3"), item: 'apple', qty: 45, status: 'A', instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a4"), item: 'pears', qty: 50, status: 'A', instock: true } ]