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.
To find documents that either match or don't match a specified schema, use $jsonSchema with query operators. Similarly, you can update or delete documents based on a schema by using $jsonSchema in query conditions for write operations.
Examples
Create a sample collection inventory with the following documents:
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:
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:
itemmust be BSON typestring.qtymust be BSON typeinteger.instockmust be BSON typeboolean.
size, if present:Must be BSON type
object.Must include
unitas a requiredstringfield.If the embedded
handwfields are present, they must be typedouble.
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:
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:
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 } ]