Docs Menu

Docs HomeView & Analyze DataMongoDB 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.18.

The validation editor supports JSON Schema validation, and validation with query expressions using query operators. As you edit your validation, Compass updates in real-time 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: ['customer'], // the customer field is required
properties: {
purchaseMethod: {
enum: ['In Store','Online'],
description: "can only be either 'In Store' or 'Online'"
}
}
}
}

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: [ "name", "year", "major", "gpa", "address.city", "address.street" ],
properties: {
name: {
bsonType: "string",
description: "must be a string"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
exclusiveMaximum: false,
description: "must be an integer in [ 2017, 3017 ]"
},
major: {
bsonType: "string",
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values"
},
gpa: {
bsonType: [ "double" ],
minimum: 0,
description: "must be a double"
}
}
}
}

This validation specifies:

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: [
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}

Using this validation, one of the following must be true:

  • The phone field must be BSON type string,

  • The email field must match the regex /@mongodb\.com$/, or

  • The status field must be either Unknown or Incomplete.

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 a Data Lake.

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

← Export Your Schema