Navigation

Validation

Note

Schema validation is not available in Compass Community Edition.

Creating, editing and dropping schema validation rules is not permitted in MongoDB Compass Readonly Edition. In Readonly Edition, validation rules may only be read.

Validation Tab

The Validation tab displays the schema validation specification (the validation rules, the validation level, and the validation action) that exists 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 document fields.

From this tab, you can update the schema validation specification via the Rule Builder or the JSON editor.

To view and modify the Validation for a collection, click on the collection on the left hand pane, then click the Validation tab.

../_images/validation-view.png

Considerations

  • If the current validation rules cannot be expressed through the Rule Builder, Compass deactivates the Rule Builder, and you can only modify the validation specification through the JSON editor view.
  • Each rule applies to a single field in the collection.
  • No two rules in the Rule Builder can have the same field.
  • The Rule Builder combines validation rules for the different fields as a logical AND.
  • To specify conditions not supported by the Rule Builder or to join validation rules with a logical OR, use the JSON Editor instead of the Rule Builder.

Validation Actions and Levels

You can specify Validation Actions and Validation Levels in the Validation tab. These settings dictate how rule violations are handled and the types of documents affected by validation.

Validation Actions

Validation actions determine how MongoDB handles documents that violate validation rules. The following table describes the different validation actions available:

Validation Action Description
Warning Allows invalid documents but warns about violations.
Error Does not allow invalid documents. With this setting, MongoDB errors and rejects documents that violate validation rules.

Validation Levels

Validation levels determine how strictly MongoDB applies validation rules to existing documents during an update. The following table describes the different validation levels available:

Validation Level Description
Off Disables validation entirely.
Moderate Applies validation rules to inserts and to updates to existing documents that already fulfill the validation criteria. With the Moderate level, updates to existing documents that do not fulfill the validation criteria are not checked for validity.
Strict Applies validation rules to all inserts and updates.

Create Validation Rules

MongoDB Compass supports two methods to create validation rules, the Rule Builder and the JSON Editor.

Rule Builder

The Rule Builder facilitates the building and modification of schema validation rules; however, the Rule Builder only supports a subset of validation conditions.

1

Update Validation Action and Validation Level.

Select the appropriate Validation Action and Validation Level.

For more information on validation actions and levels, see this section. Additional information is available in the MongoDB Manual.

2

Add, Modify, Remove a Rule.

To add a new rule, click the Add Rule button.

When adding or modifying a rule, select the field and the rule to apply to the field. The following rule categories are available via the Rule Builder:

Rule Category Description
Exists / Must Not Exist Enforces the presence / absence of a field in the documents.
Type Enforces a specific BSON Type for a field in the documents.
Range Enforces a numeric range for a field in the documents.
Regex Enforces a regular expression match for a string field in the documents.

For a rule, you can also select the Nullable checkbox to indicate that the rule is also fulfilled if the value is null or the field is not present in the document. With the Rule Builder, you cannot create compound rules for a single field other than by the Nullable checkbox.

The rule categories Exists and Must not Exist do not support the Nullable checkbox.

To delete a rule, click on the trash can icon for the rule.

3

Click Update to save changes to the validation specification.

JSON Editor

You can also use the JSON editor to create validation rules. You can use the JSON editor to create more advanced validation rules which the Rule Builder may not support.

The JSON editor supports validation with query filter expressions using the query operators, with the exception of $near, $nearSphere, $text, and $where.

1

Update Validation Action and Validation Level.

Select the appropriate Validation Action and Validation Level.

For more information on validation actions and levels, see this section. Additional information is available in the MongoDB Manual.

2

Specify the validation rules.

In the editor, enter the validation rules in strict JSON format.

Example

The following rule only allows documents where either the BeginDate field is greater than 1950 or the EndDate field is null.

{
   "$or": [
      { "BeginDate": { "$gt": 1950 } },
      { "EndDate": null }
   ]
}

Note

If you specify a rule in the JSON editor which cannot be displayed in the Rule Builder, you will not be able to access the Rule Builder.

3

Verify JSON.

When finished editing, hit the Tab key or click outside the editor box.

If the JSON document is invalid, an error message displays. Fix any errors.

4

Click Update to create/update validation rules.