Database Manual / Data Modeling / Schema Validation

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. You can query for invalid documents to potentially update or delete them from your collection.同样,如果模式的validationActionwarn,则允许集合包含无效文档。您可以查询无效文档,以便从集合中更新或删除它们。

To find documents that either match or don't match a specified schema, use $jsonSchema with query operators. 要查找与指定架构匹配或不匹配的文档,请使用带有查询运算符的$jsonSchemaSimilarly, 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: 25, size: { h: 14, w: 21, unit: "cm" }, instock: true },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, unit: "in" }, instock: true },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, unit: "in" }, instock: 1 },
{ item: "apple", qty: 45, status: "A", instock: true },
{ item: "pears", qty: 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: [ "unit" ],
properties: {
unit: { bsonType: "string" },
h: { bsonType: "double" },
w: { bsonType: "double" }
}
},
instock: { bsonType: "bool" }
}
}
}

The schema enforces the following validation:架构强制执行以下验证:

  • Required fields:必填字段:

    • item must be BSON type string.必须是BSON类型的string
    • qty must be BSON type integer.必须是BSON类型的integer
    • instock must be BSON type boolean.必须是BSON类型的boolean
  • size, if present:,如果存在:

    • Must be BSON type object.必须是BSON类型的object
    • Must include unit as a required string field.必须将unit作为必填string字段。
    • If the embedded h and w fields are present, they must be type double.如果存在嵌入的hw字段,则它们必须为double类型。

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 documents in a collection that don't match the schema validation rules, use $jsonSchema with the $nor operator. For example:要查找集合中不符合模式验证规则的文档,请使用$jsonSchema$nor运算符。例如:

db.inventory.find( { $nor: [ myschema ] } )

Output:输出:

[
// Neither size.h nor size.w are type double
{
_id: ObjectId("62b5cd5a14b92d148400f79e"),
item: 'journal',
qty: 25,
size: { h: 14, w: 21, unit: 'cm' },
instock: true
},
// size.w is not a double
{
_id: ObjectId("62b5cd5a14b92d148400f79f"),
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, unit: 'in' },
instock: true
},
// size.w is not a double and instock is not a boolean
{
_id: ObjectId("62b5cd5a14b92d148400f7a0"),
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, unit: 'in' },
instock: 1
}
]

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, unit: 'cm' },
instock: true,
isValid: false
},
{
_id: ObjectId("62b5cd5a14b92d148400f79f"),
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, unit: 'in' },
instock: true,
isValid: false
},
{
_id: ObjectId("62b5cd5a14b92d148400f7a0"),
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, unit: 'in' },
instock: 1,
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了解更多