MongoDb - MongoDB Schema Validation Rules

MongoDB is a schema-flexible database, meaning documents within the same collection do not have to follow a fixed structure. While this flexibility is one of MongoDB’s greatest strengths, it can also lead to inconsistent data if not properly managed. To address this issue, MongoDB provides Schema Validation Rules, which allow developers to define constraints that documents must satisfy before they are inserted or updated in a collection.

Schema validation helps maintain data integrity, improves application reliability, and reduces the chances of storing invalid or incomplete data.

What is Schema Validation?

Schema validation is a mechanism that checks whether a document conforms to predefined rules before it is written to the database. If a document violates these rules, MongoDB can either reject the operation or issue a warning depending on the configuration.

For example, in a student collection, you may require:

  • Every student document must contain a name.

  • Age must be a number.

  • Email must be a string.

  • GPA must be between 0 and 10.

Without validation, developers could accidentally store inconsistent data, such as text values in numeric fields or missing mandatory information.

Why Schema Validation is Important

Schema validation offers several benefits:

Data Consistency

All documents follow a standardized structure, making data easier to manage and analyze.

Improved Data Quality

Incorrect or incomplete records are prevented from entering the database.

Reduced Application Errors

Applications can rely on consistent document formats, reducing runtime exceptions.

Easier Maintenance

Developers can understand the expected document structure without examining thousands of records.

Better Security

Sensitive fields can be validated to ensure they meet required formats and constraints.

JSON Schema Validation

MongoDB primarily uses JSON Schema for document validation. JSON Schema is a standard format for describing the structure and constraints of JSON documents.

When creating a collection, developers can specify validation rules using the $jsonSchema operator.

Example Collection Creation

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: ["name", "age", "email"],
         properties: {
            name: {
               bsonType: "string",
               description: "Name must be a string"
            },
            age: {
               bsonType: "int",
               minimum: 18,
               maximum: 60
            },
            email: {
               bsonType: "string"
            }
         }
      }
   }
})

This schema ensures:

  • The document must be an object.

  • Name, age, and email are mandatory.

  • Name must be a string.

  • Age must be an integer between 18 and 60.

  • Email must be stored as a string.

Understanding Schema Components

bsonType

The bsonType field specifies the expected BSON data type.

Examples include:

bsonType: "string"
bsonType: "int"
bsonType: "bool"
bsonType: "date"

Common BSON types include:

Type Description
string Text values
int Integer numbers
double Decimal numbers
bool True or false
date Date and time
object Embedded document
array Array of values
objectId MongoDB document identifier

Required Fields

The required keyword specifies fields that must exist.

Example:

required: ["name", "email"]

A document missing either field will be rejected.

Properties

The properties section defines validation rules for individual fields.

Example:

properties: {
   age: {
      bsonType: "int"
   }
}

Validating Numeric Ranges

MongoDB allows restrictions on numerical values.

Example:

score: {
   bsonType: "int",
   minimum: 0,
   maximum: 100
}

Valid:

{
   score: 85
}

Invalid:

{
   score: 120
}

String Length Validation

Developers can control minimum and maximum string lengths.

Example:

username: {
   bsonType: "string",
   minLength: 5,
   maxLength: 20
}

Valid usernames:

"johnsmith"
"user123"

Invalid username:

"abc"

because it contains fewer than 5 characters.

Pattern Matching with Regular Expressions

MongoDB supports pattern validation using regular expressions.

Example:

email: {
   bsonType: "string",
   pattern: "^.+@.+$"
}

Valid:

[email protected]

Invalid:

userexample.com

This helps ensure data follows specific formats.

Array Validation

Arrays can also be validated.

Example:

skills: {
   bsonType: "array"
}

More detailed validation:

skills: {
   bsonType: "array",
   minItems: 1,
   maxItems: 10
}

This ensures:

  • At least one skill exists.

  • No more than ten skills are stored.

Embedded Document Validation

MongoDB documents often contain nested documents.

Example document:

{
   name: "John",
   address: {
      city: "New York",
      zipCode: "10001"
   }
}

Validation rule:

address: {
   bsonType: "object",
   required: ["city", "zipCode"],
   properties: {
      city: {
         bsonType: "string"
      },
      zipCode: {
         bsonType: "string"
      }
   }
}

This ensures the embedded document follows the required structure.

Modifying Existing Collection Validation

Validation rules can be added or modified later using the collMod command.

Example:

db.runCommand({
   collMod: "students",
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: ["name"]
      }
   }
})

This updates validation settings for the existing collection.

Validation Levels

MongoDB supports different validation levels.

Strict

All inserts and updates must follow validation rules.

validationLevel: "strict"

Moderate

Only newly inserted or updated documents are validated.

validationLevel: "moderate"

Validation Actions

MongoDB provides two validation actions.

Error

Reject invalid documents.

validationAction: "error"

Example result:

Document failed validation

Warn

Allow the document but generate a warning.

validationAction: "warn"

This is useful during migration periods.

Real-World Example: Employee Management System

Consider an employee database where each employee must have:

  • Employee ID

  • Name

  • Department

  • Salary

Validation schema:

{
   $jsonSchema: {
      bsonType: "object",
      required: [
         "employeeId",
         "name",
         "department",
         "salary"
      ],
      properties: {
         employeeId: {
            bsonType: "string"
         },
         name: {
            bsonType: "string"
         },
         department: {
            bsonType: "string"
         },
         salary: {
            bsonType: "double",
            minimum: 0
         }
      }
   }
}

This prevents:

  • Missing employee IDs.

  • Negative salaries.

  • Incorrect data types.

Best Practices for Schema Validation

Keep Validation Practical

Avoid overly restrictive rules that make future changes difficult.

Validate Critical Fields

Focus on important fields such as identifiers, contact details, and financial information.

Use Nested Validation Carefully

Deep validation can increase complexity and maintenance effort.

Combine Application and Database Validation

Application-level validation improves user experience, while database-level validation guarantees data integrity.

Document Your Schemas

Maintain clear documentation so developers understand collection requirements.

Conclusion

MongoDB Schema Validation Rules provide a powerful way to enforce structure and maintain data quality in a schema-flexible database. Using JSON Schema, developers can define required fields, data types, value ranges, string patterns, array constraints, and nested document rules. Although MongoDB allows flexible document structures, schema validation ensures consistency, reliability, and integrity across applications. By implementing well-designed validation rules, organizations can prevent invalid data from entering their databases while still benefiting from MongoDB's flexibility and scalability.