DEV Community

PRIAN S S 24CB042
PRIAN S S 24CB042

Posted on

πŸ’» Mastering MongoDB CRUD Operations β€” A Hands-on Student Database Example πŸŽ“

πŸ”° Introduction:

MongoDB is one of the most popular NoSQL databases used by developers today for building modern, scalable applications πŸš€.

Unlike traditional relational databases, MongoDB stores data in flexible JSON-like documents, making it super easy to handle real-world data without worrying about rigid table structures πŸ“„.

In this blog, I’ll walk you through performing CRUD operations (Create, Read, Update, Delete) in MongoDB using a simple example β€” a college student database πŸ§‘β€πŸŽ“

We’ll:
βœ… Insert student details
βœ… Query records
βœ… Update academic information
βœ… Delete records

All of this will be done directly on a MongoDB Atlas Cluster (cloud-based MongoDB) ☁️.

Along the way, I’ll include screenshots of my Atlas dashboard and query results so you can follow along visually πŸ‘€.

🎯 Outcome:

By the end of this blog, you’ll learn how to:

✨ Insert multiple documents into a collection
✨ Read and filter records using queries
✨ Update single and multiple documents
✨ Delete documents based on conditions
✨ Understand how CRUD fits into real-world applications

βš™οΈ Setup: Creating a Cluster

1️⃣ Create a free MongoDB Atlas account
2️⃣ Create a cluster and a database called collegeDB
3️⃣ Inside it, create a collection named students

🧩 CREATE (Insert):

Let’s begin by inserting student records into our collection.
Each student will be represented as a separate document in the students collection.

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

{
student_id: "S002",
name: "Santhosh",
age: 19,
department: "CSE",
year: 3,
cgpa: 8
}

{
student_id: "S003",
name: "SUGESH",
age: 20,
department: "EEE",
year: 4,
cgpa: 9
}

{
student_id: "S004",
name: "BOOPATHY",
age: 19,
department: "CSBS",
year: 1,
cgpa: 9.3
}

{
student_id: "S005",
name: "RAKSHANTH",
age: 20,
department: "CIVIL",
year: 2,
cgpa: 6.8
}


πŸ” READ (Query):

Let’s now retrieve and filter student data.

πŸ“‹ Display all student records:
db.students.find({})

πŸ“ˆ Find all students with CGPA > 8:
db.students.find({ cgpa: { $gt: 8 } })


πŸ’‘ Find students belonging to the Computer Science department:

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

✏️ UPDATE:

Let’s modify existing records in our collection.

Update CGPA of a specific student:
db.students.updateOne(
{ student_id: "S005" },
{ $set: { cgpa:7.5}
)

Increase the year of study for all 3rd-year students by 1:
db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
)

❌ DELETE:

Time to clean up some records
πŸ—‘οΈ Delete one student by student_id:
db.students.deleteOne({ student_id: "S004" })

πŸ—‘οΈ Delete all students with CGPA < 7.5:
db.students.deleteMany({ cgpa: { $lt: 7.5 } })

🏁 Conclusion

In this blog, we explored CRUD operations in MongoDB using a practical student database example.

We learned how to:
πŸ”Έ Insert student documents(C)
πŸ”Έ Query data with filters(R)
πŸ”Έ Update specific or multiple records(U)
πŸ”Έ Delete unwanted entries(D)

CRUD forms the foundation of every real-world application, whether managing users, products, or students πŸ‘¨β€πŸ’».

This step-by-step guide helped me understand how MongoDB handles data dynamically and efficiently, especially in academic and enterprise systems.

A huge thanks πŸ™ to @santhoshnc sir for his constant guidance and motivation throughout this learning journey. 🌟

Top comments (0)