Docs HomeMongoDB Manual

Query for and Modify Valid or Invalid Documents查询和修改有效或无效文档

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. 类似地,如果您的架构的validationActionwarn,则允许您的集合包含无效文档。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来基于模式更新或删除文档。

Examples实例

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 Schema Object定义架构对象

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

Find Documents that Match the Schema查找与架构匹配的文档

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

Find Documents that Don't Match the Schema查找与架构不匹配的文档

To find all documents do not satisfy the schema:, use $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
}
]

Update Documents that Don't Match the Schema更新与架构不匹配的文档

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

Delete Documents that Don't Match the Schema删除与架构不匹配的文档

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

Learn More了解更多信息