Schema validation lets you create validation rules for your fields, such as allowed data types and value ranges.
MongoDB uses a flexible schema model, which means that documents in a collection do not need to have the same fields or data types by default. Once you've established an application schema, you can use schema validation to ensure there are no unintended schema changes or improper data types.
Your schema validation needs depend on how users use your application. When your application is in the early stages of development, schema validation may impose unhelpful restrictions because you don't know how you want to organize your data. Specifically, the fields in your collections may change over time.
Schema validation is most useful for an established application where you have a good sense of how to organize your data. You can use schema validation in the following scenarios:
After you add schema validation rules to a collection:
To find documents in a collection that don't match the schema validation rules, see Find Documents that Don't Match the Schema .
By default, when an insert or update operation would result in an invalid document, MongoDB rejects the operation and does not write the document to the collection.
Alternatively, you can configure MongoDB to allow invalid documents and log warnings when schema violations occur.
For common tasks involving schema validation, see the following pages:
To learn about MongoDB's flexible schema model, see Data Modeling .