MongoDB: Schema Validations

 

MongoDB provides a feature called schema validation that allows you to enforce a structure and rules on the documents stored in a collection. Schema validation ensures that the data adheres to a specific schema, preventing the insertion of invalid or inconsistent documents. It helps maintain data integrity and consistency within your MongoDB database.

To enable schema validation, you need to define a JSON schema that describes the expected structure and rules for your documents. This JSON schema is then applied to the collection during the validation process.

Here’s an example of how to use MongoDB schema validation:

  1. Connect to your MongoDB database using a MongoDB client or the MongoDB shell.
  2. Choose the collection for which you want to enable schema validation.
  3. Define a JSON schema that describes the expected structure and rules for the documents in the collection. The schema can include field types, required fields, enumeration values, regular expressions, and more.

For example, let’s say you have a collection called “users” and you want to ensure that each document has a “name” field of type string and an “age” field of type number:

 

{
$jsonSchema: {
bsonType: "object",
required: ["name", "age"],
properties: {
name: {
bsonType: "string"
},
age: {
bsonType: "number"
}
}
}
}

Once you have defined the JSON schema, you can enable it on the collection using the collMod command in the MongoDB shell or an equivalent command in your MongoDB client.

db.runCommand({
collMod: "users",
validator: {
$jsonSchema: {
// JSON schema definition here
}
},
validationLevel: "strict",
validationAction: "error"
})

In the above example, we set validationLevel to “strict” and validationAction to “error”. This means that if a document doesn’t conform to the defined schema, MongoDB will raise an error and reject the insertion or update operation.

Now, whenever you insert or update a document in the “users” collection, MongoDB will validate it against the defined schema. If the document doesn’t match the schema, MongoDB will prevent the operation and return an error.

 

Certainly! Here are a few more examples illustrating different combinations of schema validations in MongoDB:

  1. Enforcing Field Types and Enumerations:

Let’s say you have a collection called “products” and you want to enforce that each document has a “name” field of type string and a “category” field that can only have specific values, such as “electronics,” “clothing,” or “books.”

{
$jsonSchema: {
bsonType: "object",
required: ["name", "category"],
properties: {
name: {
bsonType: "string"
},
category: {
bsonType: "string",
enum: ["electronics", "clothing", "books"]
}
}
}
}
  1. Setting Minimum and Maximum Values:

Suppose you have a collection called “employees” and you want to ensure that the “salary” field is a number and falls within a specific range, such as a minimum of 1000 and a maximum of 10000.

{
$jsonSchema: {
bsonType: "object",
required: ["name", "salary"],
properties: {
name: {
bsonType: "string"
},
salary: {
bsonType: "number",
minimum: 1000,
maximum: 10000
}
}
}
}
  1. Validating String Patterns:

Let’s say you have a collection called “emails” and you want to enforce that the “address” field is a string and follows a specific email address pattern.

{
$jsonSchema: {
bsonType: "object",
required: ["address"],
properties: {
address: {
bsonType: "string",
pattern: "^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}$"
}
}
}
}

In the above example, the pattern ^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$ ensures that the “address” field follows the basic structure of an email address.

These examples demonstrate a few ways you can combine different validations in MongoDB schema. You can customize the schema based on your specific requirements, combining various field types, required fields, enumerations, patterns, and more, to enforce data consistency and integrity.

  1. Lenient Validation:
{
$jsonSchema: {
bsonType: "object",
required: ["name"],
properties: {
name: {
bsonType: "string"
}
}
}
}

In this example, the schema enforces that each document in the collection must have a “name” field of type string, marked as required. However, the validationLevel is set to “moderate” (default) and validationAction is set to “warn”. This means that MongoDB will generate a warning but still allow the insertion or update of documents that do not conform to the schema.

  1. Strict Validation with Error Handling:
{
$jsonSchema: {
bsonType: "object",
required: ["name"],
properties: {
name: {
bsonType: "string"
}
}
}
}

In this example, the schema enforces that each document must have a “name” field of type string, marked as required. The validationLevel is set to “strict” and validationAction is set to “error”. If a document does not conform to the schema, MongoDB will raise an error and reject the insertion or update operation.

  1. Silent Validation:
{
$jsonSchema: {
bsonType: "object",
required: ["name"],
properties: {
name: {
bsonType: "string"
}
}
}
}

In this example, the schema enforces that each document must have a “name” field of type string, marked as required. The validationLevel is set to “off” and validationAction is set to “warn”. This means that MongoDB will not perform any validation and will not raise any errors or warnings. It allows the insertion or update of documents without any validation checks.

These examples demonstrate different combinations of validationLevel and validationAction options in MongoDB schema validation. You can choose the appropriate level of strictness and error handling based on your application’s requirements. Remember to carefully consider the impact on data consistency and performance when selecting the validation options.

When enabling schema validation on an existing collection in MongoDB, you have a few options for handling the existing documents that may not conform to the defined schema. Let’s explore these options:

  1. Validation Only Future Documents: By default, when you enable schema validation on a collection, it only applies to future insert and update operations. Existing documents are not validated against the schema. This allows you to gradually enforce the schema on new data while allowing existing data to remain unchanged.
  2. Validate Existing Documents: If you want to validate the existing documents against the schema, you can use the validate command or the validate() method in the MongoDB shell or your MongoDB client. This command allows you to explicitly trigger the validation process on all existing documents in the collection.

Example:

db.collection.validate({ full: true })

The full: true option ensures that all existing documents in the collection are validated against the schema. If any documents fail the validation, MongoDB will return an error message with details about the invalid documents.

  1. Update Existing Documents to Conform to the Schema: If you have existing documents that do not conform to the defined schema, you can update them to align with the schema. This might involve adding missing fields, correcting data types, or making other adjustments to ensure compliance.

You can use the $set operator in an update operation to modify the existing documents. Here’s an example that updates all documents in a collection to include a missing “email” field:

db.collection.updateMany(
{ email: { $exists: false } }, // Find documents that do not have the "email" field
{ $set: { email: "" } } // Add the "email" field with an empty string value
)

By updating the existing documents, you bring them in line with the defined schema.

It’s important to note that schema validation is not a one-time process. Once enabled, MongoDB continuously validates new documents against the schema as they are inserted or updated. It’s recommended to thoroughly test and validate your schema before enabling it to ensure a smooth transition and minimize disruption to your existing data.

Remember to back up your data before performing any operations that modify existing documents, as they can have irreversible consequences if not handled carefully.

 

Schema validation in MongoDB is a powerful feature that allows you to enforce data consistency and integrity. By defining a JSON schema for your collections, you can ensure that your data meets specific requirements and maintain a standardized structure across documents.

It’s important to note that schema validation can impact the performance of write operations, as MongoDB needs to perform additional checks. Therefore, it’s crucial to carefully design your schema and consider the trade-off between data validation and performance.

  • Ask Question