DEV Community

Santhosh_M
Santhosh_M

Posted on

CRUD OPERATIONS










Mastering MongoDB CRUD with a Simple College Student Schema πŸŽ“

Databases are the backbone of modern applications β€” and MongoDB, with its flexible NoSQL design, has become a developer favorite.

In this blog, I’ll walk you through CRUD (Create, Read, Update, Delete) operations in MongoDB Atlas, using a simple yet relatable example: managing college student records.

πŸ“Œ Schema: students Collection

{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}

  1. Create (Insert)

Insert 5 student records:

db.students.insertMany([
{ "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
{ "student_id": "S002", "name": "Priya", "age": 21, "department": "CSE", "year": 3, "cgpa": 8.7 },
{ "student_id": "S003", "name": "Arjun", "age": 19, "department": "ECE", "year": 1, "cgpa": 7.2 },
{ "student_id": "S004", "name": "Meena", "age": 22, "department": "CSE", "year": 3, "cgpa": 9.1 },
{ "student_id": "S005", "name": "Vikram", "age": 20, "department": "IT", "year": 2, "cgpa": 8.0 }
])

βœ” Inserts multiple documents at once.

  1. Read (Query)

πŸ‘‰ Display all records

db.students.find().pretty()

πŸ‘‰ Find students with CGPA > 8

db.students.find({ cgpa: { $gt: 8 } })

πŸ‘‰ Find students in Computer Science (CSE)

db.students.find({ department: "CSE" })

  1. Update

πŸ‘‰ Update CGPA of a specific student

db.students.updateOne(
{ student_id: "S002" },
{ $set: { cgpa: 9.0 } }
)

πŸ‘‰ Increase year for all 3rd-year students

db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
)

  1. Delete

πŸ‘‰ Delete one student by student_id

db.students.deleteOne({ student_id: "S003" })

πŸ‘‰ Delete all students with CGPA < 7.5

db.students.deleteMany({ cgpa: { $lt: 7.5 } })

Deliverables

βœ… MongoDB CRUD queries (insert, find, update, delete)

βœ… Export of final collection (JSON/CSV)

Key Takeaways

CRUD in MongoDB is schema-flexible and beginner-friendly.

Using a real-world example (students) makes learning easier.

These basics set the stage for aggregations, indexing, and scaling.

Tried MongoDB CRUD with your own dataset? Share in the comments!
If you found this useful, drop a and connect with me on LinkedIn.

Top comments (0)