Schema validation lets you create validation rules for your fields, such as allowed data types and value ranges.
MongoDB uses a flexible schema model. By default, documents in a collection don't need the same fields or data types. After you establish an application schema, use schema validation to prevent unintended schema changes and data type errors.
You can implement schema validation in the UI for deployments hosted in MongoDB Atlas.
When to Use Schema Validation
Your schema validation needs depend on how your application organizes data. Schema validation is most useful for an established application with a defined data structure.
Note
Schema validation rules are also flexible, so they don't need to cover every field in a document, unless your application requires that they do.
You can use schema validation in the following scenarios:
For an
eventscollection, ensure that thestart_datefield stores only a date, not a string. Consistent types prevent unexpected values in connecting applications.For a
storecollection, ensure that theaccepted_credit_cardsfield contains only accepted card types, such as["Visa", "MasterCard", "American Express"]. This rule prevents users from entering unsupported values.For a
studentscollection, ensure that thegpafield is always a positive floating-point number. This rule prevents data entry errors.
When MongoDB Checks Validation
After you add schema validation rules to a collection:
All document inserts must match the rules.
The validation level determines how rules apply to existing documents and updates. To learn more, see Specify Validation Level for Existing Documents.
To find documents in a collection that don't match the schema validation rules, see Find Documents that Don't Match the Schema.
What Happens When a Document Fails Validation
By default, MongoDB rejects any insert or update operation that would produce an invalid document.
Alternatively, you can configure MongoDB to allow invalid documents and log a warning when a schema violation occurs.
To learn more, see Choose How to Handle Invalid Documents.
Get Started
For schema validation tasks, see the following pages:
Learn More
To learn about MongoDB's flexible schema model, see Data Modeling in MongoDB.