π° 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)