body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 20px;
background-color: #f9f9f9;
color: #333;
}
h1, h2 {
color: #333;
}
pre {
background-color: #f4f4f4;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
margin-bottom: 20px;
}
code {
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 4px;
}
.example {
background-color: #e8f5e9;
padding: 10px;
border-left: 4px solid #4caf50;
margin: 20px 0;
}
MongoDB Schema Validation
Introduction
Learn how to enforce data integrity and structure in MongoDB using schema validation rules.
Connecting to MongoDB
Before applying schema validation rules, ensure you are connected to your MongoDB server:
Example: Connecting to MongoDB
mongosh
This command starts the mongosh
shell and connects to the default MongoDB server running locally.
Switching to a Database
Switch to the database where your collection resides using the use
command:
Example: Switching to a Database
use mydatabase
This command switches to the “mydatabase” database. Replace mydatabase
with your database name.
Enabling Schema Validation
Enable schema validation for a collection using the collMod
command:
Example: Enabling Schema Validation
db.createCollection("mycollection", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "age"],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
age: {
bsonType: "int",
description: "must be an integer and is required"
}
}
}
}
})
This command creates a collection mycollection
with schema validation rules. Documents must have name
(string) and age
(integer) fields.
Validation Rules
Define validation rules using JSON schema:
- bsonType: Specifies the BSON type of the field.
- required: Specifies required fields.
- properties: Defines field properties and types.
- description: Provides a description of the field.
Validation Errors
Handle validation errors when documents do not meet schema rules:
Example: Handling Validation Errors
db.mycollection.insertOne({ name: "John" })
If the document doesn’t include the age
field, MongoDB will throw a validation error.
Updating Validation Rules
Update validation rules using the collMod
command:
Example: Updating Validation Rules
db.runCommand({
collMod: "mycollection",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "age", "email"],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
age: {
bsonType: "int",
description: "must be an integer and is required"
},
email: {
bsonType: "string",
pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
description: "must be a valid email address"
}
}
}
}
})
This command updates the schema validation rules to include the email
field with a regex pattern for email validation.
Conclusion
Schema validation in MongoDB ensures data integrity by enforcing structure and type constraints on documents. Define and update validation rules to meet your application’s requirements and maintain consistent data quality.