Set Validation Rules for Your Schema
On this page
Validation Tab
The Validation tab allows you to manage schema validation rules for a collection.
Schema validation ensures that all documents in a collection follow a defined set of rules, such as conforming to a specific shape or only allowing a specified range of values in fields.

Validation Rules
The validation editor supports JSON Schema validation, and validation with query expressions using Validation using Query Operators.
JSON Schema Validation
The $jsonSchema operator supports various keywords to specify validation rules. For example:
The
required
array defines required fields in your document.The
properties
object defines rules for specific document fields.
To generate a JSON schema by analyzing existing sample data, see Generate Validation Rules.
Example
Consider the following example validation:
{ $jsonSchema: { bsonType: "object", required: [ "address", "borough", "name" ], properties: { address: { bsonType: "object", properties: { coord: { bsonType: "array", items: [ { bsonType: "double", minimum: -180, maximum: 180, exclusiveMaximum: false, description: "must be a number in [ -180, 180 ]" }, { bsonType: "double", minimum: -90, maximum: 90, exclusiveMaximum: false, description: "must be a number in [ -90, 90 ]" } ] } }, description: "must be an object" }, borough: { bsonType: "string", enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ], description: "must be one of the enum strings" } } } }
This validation specifies:
The list of required fields.
The bsonType for all required fields.
The acceptable values for the
borough
field, using enum.
For all available $jsonSchema
keywords, refer to the
$jsonSchema page in
the MongoDB manual.
Validation using Query Operators
You can also specify validation using
query operators, with the
exception of the following query operators: $near
,
$nearSphere
, $text
, and $where
.
{ $or: [ { name: { $type: "string" } }, { borough: { bsonType: "string", enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ], description: "must be one of the enum strings" } } ] }
Using this validation, one of the following must be true:
The
name
field must be BSON type string.The
borough
field must be one of the enum strings.
Validation Actions and Levels
At the top, specify a Validation Action and Validation Level:
The validation action determines whether to
warn
but accept invalid documents, orerror
and reject invalid documents.The validation level determines how strictly MongoDB applies validation rules to existing documents.
Strict
validation applies your rules to all document inserts and updates.Moderate
validation only applies your rules to new documents and existing valid documents. Existing invalid documents are not affected.
For details on validation actions and levels, see Specify Validation Rules in the MongoDB manual.
Get Started
Limitations
The Validation tab is not available if you are connected to Atlas Data Federation.
Compass cannot validate a schema that has more than 1000 distinct fields. If you try to validate a schema with more than 1000 distinct fields, Compass returns an error.
In MongoDB Compass Readonly Edition, you can only view validation rules. Creating and editing validation rules is not permitted.