Exploring MongoDB CRUD Operations: A Beginner’s Guide
MongoDB is one of the most popular NoSQL databases out there — and for good reason! It’s built for cloud-based, scalable apps that need flexibility and speed. Instead of storing data in strict tables like traditional databases, MongoDB uses JSON-like documents that feel much closer to real-world data. For example, representing students in a college database becomes super simple and natural.
At the heart of MongoDB are the four key CRUD operations — Create, Read, Update, and Delete. These let you add new data, view it, modify it, or remove it when needed.
In this blog, we’ll get our hands dirty performing CRUD operations on a student database. You’ll learn how to:
- Insert multiple student records
- Fetch and filter data using queries
- Update details such as CGPA or academic year
- Delete specific student records
We’ll use MongoDB Atlas, the cloud-based version of MongoDB, to make everything easy and accessible. By the end, you’ll have a solid, practical understanding of how MongoDB works — and why developers love using it for modern applications.
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
}
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
}
Results:
Inserted 5 documents successfully.
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.
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.
DELETE (REMOVE RECORDS)
(a) Delete one student by ID
({ student_id: "ST005" })
(b) Delete all students with CGPA < 7.5
({ cgpa: { $lt: 7.5 } })
Top comments (0)