Validate MongoDB Documents With $jsonSchema
July 31, 2026 · DevTools
The flexibility of a document store is a double-edged sword. When any shape can land in a collection, any shape will — a missing field, a string where an integer belonged, a typo that quietly corrupts reports for weeks. MongoDB lets you push back with collection validators built from JSON Schema (via $jsonSchema), and the cheapest moment to enforce a shape is at write time, not in every downstream consumer.
What a $jsonSchema validator looks like
A MongoDB validator is a normal JSON Schema wrapped in $jsonSchema, applied with collMod:
db.runCommand({
collMod: "users",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["email", "age"],
properties: {
email: { bsonType: "string", pattern: "^[^@]+@[^@]+\\.[^@]+$" },
age: { bsonType: "int", minimum: 0, maximum: 150 },
role: { enum: ["admin", "editor", "viewer"] }
}
}
}
})
Note the two MongoDB-isms: types use bsonType aliases (int, long, double, bool, object, array…), not plain JSON types, and the wrapper key is $jsonSchema.
Build it without the typos
Authoring that by hand is error-prone — a single wrong bsonType silently lets bad data through. The JSON Schema Builder turns a form into a correct schema: add properties, pick a type, mark which are required, and add constraints (string pattern, numeric minimum/maximum, enum lists). It emits both a standard draft-07 JSON Schema and the equivalent MongoDB $jsonSchema, and it maps each type to the right bsonType automatically.
The real value is the Test tab. Paste a sample document and it validates against the generated schema live, so you confirm the validator accepts what you expect and rejects what it shouldn't before you apply it to a collection.
Validate documents you already have
If you just want to check a document against a schema — without applying anything to a collection — use the NoSQL Validator. Paste a document on one side and a JSON Schema (or $jsonSchema) on the other, and it checks every node: type, required, additionalProperties:false, array items, enum, string minLength/maxLength/pattern, and numeric minimum/maximum. Each failure points at the exact field path.
age — Expected type int, got string.
Two common mistakes to watch for: listing a property in properties does not make it required — you must also add it to the required array; and additionalProperties: false is what actually rejects stray keys, so set it when your shape is meant to be closed.
A practical workflow
- Build the schema with the builder, adding properties and constraints.
- Test sample documents — both a good one and a bad one — to confirm the behavior.
- Apply the generated
$jsonSchemawithcollMod(setvalidationLevelandvalidationActionto taste). - Re-validate individual documents anytime with the validator when you're debugging.
Everything runs in your browser — your schema and sample documents never leave the tab — so it's safe to prototype with realistic data before you touch a production collection.