Query for and Modify Valid or Invalid Documents
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. 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: 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
:
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 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:
[ { _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
:
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 } ]