
Validation in MongoDB
MongoDB allows you to enforce schema validation on your documents to ensure that the data inserted or updated into a collection adheres to a predefined structure. Schema validation helps you maintain consistency, data integrity, and avoid invalid data types or incorrect values being inserted into the database.
In MongoDB, you can define validation rules at the collection level using JSON schema syntax. These validation rules can enforce data types, required fields, field patterns, and other constraints on your documents.
1. Enabling Schema Validation
Schema validation is implemented through the validator
option during the creation of a collection or when modifying an existing collection. MongoDB uses the JSON Schema format to define validation rules.
Creating a Collection with Validation
When you create a new collection, you can specify validation rules to ensure that documents in the collection follow the desired structure.
Syntax:
db.createCollection("collection_name", { validator: { $jsonSchema: { bsonType: "object", // Document type required: [ "field1", "field2" ], // Required fields properties: { field1: { bsonType: "string" }, field2: { bsonType: "int" }, field3: { bsonType: "bool" } } } }})
Example:
db.createCollection("users", { validator: { $jsonSchema: { bsonType: "object", required: [ "name", "email", "age" ], properties: { name: { bsonType: "string" }, email: { bsonType: "string", pattern: "^.+@.+\..+$" }, // Email pattern age: { bsonType: "int", minimum: 18 }, // Age must be >= 18 isActive: { bsonType: "bool" } } } }})
- In this example, we’ve created a collection named
users
with validation rules:- The
name
,email
, andage
fields are required. - The
email
field must follow a basic email pattern. - The
age
field must be an integer greater than or equal to 18. - The
isActive
field is a boolean, but it is not required.
- The
2. Modifying Schema Validation on an Existing Collection
You can also apply or modify validation rules on an existing collection using the collMod
command. This allows you to change the validation criteria without dropping and recreating the collection.
Syntax:
db.runCommand({ collMod: "collection_name", validator: { $jsonSchema: { bsonType: "object", required: [ "field1", "field2" ], properties: { field1: { bsonType: "string" }, field2: { bsonType: "int" } } } }, validationAction: "warn" // Optionally define how to handle invalid data})
Example:
db.runCommand({ collMod: "users", validator: { $jsonSchema: { bsonType: "object", required: [ "name", "email", "age" ], properties: { name: { bsonType: "string" }, email: { bsonType: "string", pattern: "^.+@.+\..+$" }, age: { bsonType: "int", minimum: 18 }, isActive: { bsonType: "bool" } } } }, validationAction: "error" // Throw error if validation fails})
validationAction
can be set to:error
: Reject the operation if a document does not meet the validation criteria (default).warn
: Log a warning, but still allow the operation to proceed with the invalid document.
3. Validation Rules in MongoDB
MongoDB uses JSON Schema for validation, which allows you to define complex rules for your data. Some of the most commonly used schema keywords for validation are:
bsonType
: Specifies the expected BSON data type for the field. Examples include"string"
,"int"
,"double"
,"object"
,"array"
,"bool"
, etc.required
: An array that specifies which fields are required for documents in the collection.properties
: Defines the validation rules for each field in the document.pattern
: A regular expression pattern that the string field must match.minimum
andmaximum
: Specify the minimum and maximum allowable values for numeric fields.enum
: A list of acceptable values for a field.const
: Specifies that the field must always have a specific value.
4. Example Validation Rules
Required Fields:
{ bsonType: "object", required: [ "name", "age" ], properties: { name: { bsonType: "string" }, age: { bsonType: "int", minimum: 18 } }}
- In this example,
name
andage
are required fields. Theage
must be at least 18.
Email Pattern Validation:
{ bsonType: "object", required: [ "email" ], properties: { email: { bsonType: "string", pattern: "^.+@.+\..+$" // Regular expression for email pattern } }}
- The
email
field must match a basic email pattern (e.g.,user@example.com
).
Enum Validation (only specific values allowed):
{ bsonType: "object", properties: { status: { bsonType: "string", enum: [ "active", "inactive", "pending" ] } }}
- The
status
field must be one of the three specified values:"active"
,"inactive"
, or"pending"
.
Array Validation:
{ bsonType: "object", properties: { tags: { bsonType: "array", items: { bsonType: "string" }, minItems: 1, // At least one tag is required maxItems: 5 // No more than 5 tags allowed } }}
- The
tags
field must be an array of strings with at least 1 and no more than 5 items.
5. Validation Action
When a document is inserted or updated, MongoDB uses the validationAction
option to determine how to handle documents that fail validation. The two options are:
error
: If validation fails, MongoDB rejects the operation (this is the default behavior).warn
: If validation fails, MongoDB logs a warning but allows the operation to continue.
You can set the validationAction
option when creating or modifying the collection.
Example:
db.createCollection("users", { validator: { $jsonSchema: { bsonType: "object", required: [ "name", "age" ], properties: { name: { bsonType: "string" }, age: { bsonType: "int", minimum: 18 } } } }, validationAction: "warn" // Allow operation but log a warning})
6. Schema Validation and Data Integrity
Schema validation is an essential feature for enforcing consistent data structure in your MongoDB collections. Some of the benefits of schema validation include:
- Data integrity: Prevents inserting invalid or incorrectly formatted data into collections.
- Consistency: Ensures that all documents within a collection follow the same structure, which can be critical for data analysis or reporting.
- Flexibility: Schema validation allows you to define rules specific to your use case, such as validating patterns, enumerations, or numeric ranges.
However, schema validation is optional in MongoDB, and MongoDB’s flexible schema means that you can also store unstructured data. This gives you the ability to evolve your data structure over time, but using validation rules can significantly improve data quality.
7. Schema Validation with Aggregation Pipeline (MongoDB 4.2 and later)
From MongoDB 4.2 onward, you can use the aggregation pipeline in combination with the update
operation, providing additional flexibility for validation or conditional updates.
Conclusion
Schema validation in MongoDB helps you ensure data integrity by enforcing constraints on document structure and field values. By using JSON Schema, you can define rules for required fields, data types, patterns, and other field-specific validation conditions. The validator
option allows you to enforce these rules during document insertion and updates, and the validationAction
option determines how MongoDB handles invalid documents (either by rejecting them or warning you).
MongoDB’s flexible validation system ensures that you can evolve your schema while still maintaining some level of structure, making it a powerful feature for working with unstructured or semi-structured data.