MongoDB is a leading NoSQL database widely used for cloud-native and scalable applications. Unlike relational databases with rigid schemas, it stores data in flexible JSON-like documents, making it easy to model real-world entities such as students in a college database.
At its core, MongoDB supports CRUD operations — Create, Read, Update, and Delete — to add, retrieve, modify, and remove data.
In this blog, we’ll perform CRUD operations on a student database schema by:
- Inserting multiple student records
- Querying and filtering data
- Updating details like CGPA and year
- Deleting records with conditions
We’ll use MongoDB Atlas, the cloud-hosted service, for hands-on practice. By the end, you’ll have practical experience managing data in MongoDB — an essential skill for modern development.
🧱 Schema – Collection: students
Each student record (document) follows this structure:
{
"student_id": "ST001",
"name": "Aarav",
"age": 20,
"department": "AI & DS",
"year": 2,
"cgpa": 8.9
}
⚙️ 1️⃣ CREATE (INSERT)
In MongoDB Atlas → Collections → Insert Document
{
"student_id": "ST001",
"name": "Aarav",
"age": 20,
"department": "AI & DS",
"year": 2,
"cgpa": 8.9
},
{
"student_id": "ST002",
"name": "Diya",
"age": 19,
"department": "CSE",
"year": 1,
"cgpa": 9.2
},
{
"student_id": "ST003",
"name": "Rahul",
"age": 21,
"department": "ECE",
"year": 3,
"cgpa": 7.8
},
{
"student_id": "ST004",
"name": "Meera",
"age": 20,
"department": "IT",
"year": 2,
"cgpa": 9.5
},
{
"student_id": "ST005",
"name": "Vikram",
"age": 22,
"department": "MECH",
"year": 3,
"cgpa": 6.9
}
✅ Result in Atlas:
Inserted 5 documents successfully.
🔍 2️⃣ READ (QUERY)
Run the following queries
(a) Display all student records
find()
(b) Find all students with CGPA > 8
({ cgpa: { $gt: 8 } })
(c) Find students belonging to Computer Science departments
({ department: { $in: ["CSE", "AI & DS"] } })
📊 This helps identify top performers or department-based groups easily.
✏️ 3️⃣ UPDATE (MODIFY RECORDS)
(a) Update CGPA of a specific student
{ student_id: "ST002" },
{ $set: { cgpa: 9.6 } }
✅ Matched 1 document, modified 1 document.
(b) Increase the year of study for all 3rd-year students by 1
{ year: 3 },
{ $inc: { year: 1 } }
💡 $inc automatically increments numerical fields — perfect for promotions or increments.
🗑️ 4️⃣ DELETE (REMOVE RECORDS)
(a) Delete one student by ID
({ student_id: "ST005" })
OUTPUT:
(b) Delete all students with CGPA < 7.5
({ cgpa: { $lt: 7.5 } })
OUTPUT:
🧹 Removes low-performing or outdated records, keeping your data clean.
🎓 Learning Outcomes
By performing these CRUD operations, you’ll learn to:
- Work with MongoDB Atlas Cloud Interface
- Write and execute basic MongoDB queries
- Update and delete data safely
- Export and visualize your collections
Top comments (0)