Docs Menu
Docs Home
/
MongoDB Compass
/

Set Validation Rules for Your Schema

On this page

  • Validation Tab
  • Validation Rules
  • Validation Actions and Levels
  • Limitations

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 view
click to enlarge

Updated in version 1.35.1

The validation editor supports JSON Schema validation, and validation with query expressions using query operators. After you click the Update button, Compass updates to display a document from your collection that passes the validation and a document that fails.

To specify JSON Schema validation, use the $jsonSchema operator.

{
$jsonSchema: {
required: ['name', 'borough'], // the name and borough fields are required
properties: {
cuisine: {
bsonType: "string",
description: "must be a string"
}
}
}
}

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.

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 minimum and maximum values in the address.coord array.

  • The acceptable values for the borough field, using enum.

For all available $jsonSchema keywords, refer to the $jsonSchema page in the MongoDB manual.

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.

At the top, specify a Validation Action and Validation Level:

  • The validation action determines whether to warn but accept invalid documents, or error 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.

Tip

See also:

The Validation tab is not available if you are connected to Atlas Data Federation.

In MongoDB Compass Readonly Edition, you can only view validation rules. Creating and editing validation rules is not permitted.

Back

Performance Insights