MongoDB Schema Validation : A Beginner’s Guide
MongoDB Schema Validation Explained with Practical Example
Introduction
MongoDB is a NoSQL database that allows flexible document structures. However, to ensure data consistency and quality, we can apply schema validation. In this blog, I’ll explain how I implemented schema validation on a student performance dataset using MongoDB Compass, step by step.
What is Schema Validation?
MongoDB allows inserting documents without enforcing a strict schema, but in real-world applications, we often need rules to control the data structure.Schema validation helps ensure:
Only allowed data types are stored (e.g., string, int)
Certain fields are required
Values stay within expected ranges
Invalid documents are rejected before insertion
Dataset Overview
The dataset used was about student performance, including fields like:
Hours\_Studied
Attendance
Sleep\_Hours
Exam\_Score
Internet\_Access
Gender
And more (total of 20 fields)
This data helps understand what factors impact exam scores.
Steps to Apply Schema Validation in MongoDB Compass
Step 1: Open MongoDB Compass
Connect to your database and select the collection. In this case, the collection is named `student_performance`.
Step 2: Navigate to the “Validation” Tab
Click the “Validation” tab at the top.
Step 3: Add the Validation Schema
Paste your JSON schema into the textbox. Here's the schema I used:
json
{
"$jsonSchema": {
"bsonType": "object",
"required": [
"Hours_Studied", "Attendance", "Parental_Involvement",
"Access_to_Resources", "Sleep_Hours", "Previous_Scores",
"Motivation_Level", "Internet_Access", "Tutoring_Sessions",
"Family_Income", "Teacher_Quality", "School_Type",
"Peer_Influence", "Physical_Activity", "Learning_Disabilities",
"Parental_Education_Level", "Distance_from_Home", "Gender",
"Exam_Score"
],
"properties": {
"Hours_Studied": { "bsonType": "int", "minimum": 0, "maximum": 30 },
"Attendance": { "bsonType": "int", "minimum": 0, "maximum": 100 },
"Parental_Involvement": { "bsonType": "string", "enum": ["Low", "Medium", "High"] },
"Access_to_Resources": { "bsonType": "string", "enum": ["Low", "Medium", "High"] },
"Sleep_Hours": { "bsonType": "int", "minimum": 4, "maximum": 12 },
"Previous_Scores": { "bsonType": "int", "minimum": 0, "maximum": 100 },
"Motivation_Level": { "bsonType": "string", "enum": ["Low", "Medium", "High"] },
"Internet_Access": { "bsonType": "string", "enum": ["Yes", "No"] },
"Tutoring_Sessions": { "bsonType": "int", "minimum": 0 },
"Family_Income": { "bsonType": "string", "enum": ["Low", "Medium", "High"] },
"Teacher_Quality": { "bsonType": "string", "enum": ["Low", "Medium", "High"] },
"School_Type": { "bsonType": "string", "enum": ["Public", "Private"] },
"Peer_Influence": { "bsonType": "string", "enum": ["Negative", "Neutral", "Positive"] },
"Physical_Activity": { "bsonType": "int", "minimum": 0, "maximum": 7 },
"Learning_Disabilities": { "bsonType": "string", "enum": ["Yes", "No"] },
"Parental_Education_Level": { "bsonType": "string", "enum": ["High School", "College", "Postgraduate"] },
"Distance_from_Home": { "bsonType": "string", "enum": ["Near", "Moderate", "Far"] },
"Gender": { "bsonType": "string", "enum": ["Male", "Female"] },
"Exam_Score": { "bsonType": "int", "minimum": 35, "maximum": 100 }
}
}
}
Click **Confirm** after pasting the schema.
Testing the Schema
Valid Document Example
json
{
"Hours_Studied": 22,
"Attendance": 92,
"Parental_Involvement": "Medium",
"Access_to_Resources": "High",
"Extracurricular_Activities": "Yes",
"Sleep_Hours": 7,
"Previous_Scores": 88,
"Motivation_Level": "High",
"Internet_Access": "Yes",
"Tutoring_Sessions": 2,
"Family_Income": "Medium",
"Teacher_Quality": "High",
"School_Type": "Private",
"Peer_Influence": "Positive",
"Physical_Activity": 3,
"Learning_Disabilities": "No",
"Parental_Education_Level": "College",
"Distance_from_Home": "Moderate",
"Gender": "Female",
"Exam_Score": 81
}
This document inserts successfully because it follows all validation rules.
Invalid Document Example
json
{
"Hours_Studied": -5,
"Attendance": 105,
"Parental_Involvement": 2,
"Access_to_Resources": "Extreme",
"Extracurricular_Activities": "Sometimes",
"Sleep_Hours": "Seven",
"Previous_Scores": 89,
"Motivation_Level": "Medium",
"Internet_Access": "Maybe",
"Tutoring_Sessions": -1,
"Family_Income": "High",
"Teacher_Quality": "Medium",
"School_Type": "Govt",
"Peer_Influence": "Negative",
"Physical_Activity": 3,
"Learning_Disabilities": "Unknown",
"Parental_Education_Level": "Illiterate",
"Distance_from_Home": "Very Far",
"Gender": "Other",
"Exam_Score": 110
}
MongoDB Compass will reject this document with an error message because several fields violate the schema rules.
Conclusion
MongoDB Schema Validation is essential when working with structured data in dynamic applications. It prevents invalid data from being saved, ensures quality, and supports future data analysis and reporting.
If you're working with MongoDB in your projects, adding validation rules will help you maintain consistency and reliability from day one.
TEJAS MUSMADE
University: Shree Balaji University, Pune
School: School of Computer Studies
Course: BCA (Bachelor of Computer Applications)
Interests: NoSQL, MongoDB, and related technologies
Comments
Post a Comment