MongoDB’s flexible document model allows for a wide range of data types within a collection, offering an vast amount of versatility. However, in certain scenarios, it becomes imperative to maintain a structured schema across all documents, while retaining the benefits of MongoDB’s inherent flexibility. This is where document schema validation proves to be a powerful tool.
Understanding Schema Validation
Schema validation in MongoDB enables the enforcement of a predefined schema on all documents inserted into a collection. While the default behavior of MongoDB allows for a mix of types within a document, schema validation ensures that each document adheres to a specified structure. This becomes particularly crucial in use cases where maintaining a consistent schema is essential.
Maybe you have certain core fields of a document that you need to be present, in order to use that document within your application, such as a type
field. You can be assured any documents inserted in your collection always have at least a type
field, and that the type
field is of a certain data type by using schema validation.
Field-Level Validation
Schema validation operates at the field level, allowing you to define rules for individual fields within the document. For example, it can enforce that a field must be a number rather than a “number as a string,” or it can mandate that a field is always a positive number.
On a data type level, the integer number
1
, decimal1.0
and string"1"
are not the same, even if some programming languages make that assumption. Additionally it can be (sadly) very easy to mix up data types while coding and if you do, you might create documents with data that won’t match queries you run later. Schema validation can help avoid this, if your code does not.
Additionally, validation can be helpful to ensure that a password field is a string of a specific minimum length, or that a field can only take one of a few predetermined values (such as a enum field).
Basic Schema Validation
Let’s consider a basic example to illustrate how schema validation works. Assume we’re working with a collection of recipes, and we want to ensure that each document always includes at least a title
and a type
field. The initial validation rule might look like this:
{
"$jsonSchema": {
"required": ["title", "type"]
}
}
Implementing Schema Validation
Applying schema validation to a collection is straightforward. During the collection creation or modification, use the validator
option:
db.createCollection("myCollection", {
"validator": {
"$jsonSchema": { }
}
})
The $jsonSchema
option takes a document, allowing you to define the validation rules, as demonstrated in the preceding examples. So, if you know your rules at the time you create your collection you can apply them like so:
db.createCollection("cookbook", {
validator: {
"$jsonSchema": {
"required": ["title", "type"]
}
}
})
This ensures that every document in the collection contains the specified fields, but does not impose any other restrictions on their fields. You can also use the MongoDB Compass UI to apply validation.
If you need to add these rules after creating your collection you can modify it by using runCommand
and collMod
on the collection:
db.runCommand( { collMod: "cookbook",
validator: {
"$jsonSchema": {
"required": ["title", "type"]
}
}
})
To add further constraints and validation information, we can define the properties of the schema:
{
"$jsonSchema": {
required: ["title", "type"],
properties: {
"title": {
bsonType: "string",
description: "Must be a string and is required"
},
"type": {
enum: ["Breakfast", "Dinner", "Dessert"],
description: "Must be a valid type and is required"
}
}
}
}
In this example, we specify that the title
field must be a string, and the type
field can only have values from the specified enum. By providing a value for description
, error messages can be included to guide developers attempting to insert or modify documents in an invalid manner.
Conclusion
Document schema validation in MongoDB provides developers with a powerful mechanism to ensure data consistency and integrity within a collection. While MongoDB’s flexibility is advantageous in many scenarios, schema validation offers a balance by allowing you to define and enforce specific rules for your data. Consider implementing schema validation in situations where maintaining a structured schema is paramount to your application’s success!