MongoDB is one of the most popular NoSQL databases, widely used for scalable and flexible applications. In this tutorial, we’ll perform CRUD (Create, Read, Update, Delete) operations on a college students collection to understand MongoDB better.
To make it more exciting, we’ll run these queries directly on MongoDB Atlas Cluster (cloud-based MongoDB). Along the way, I’ll include screenshots of my MongoDB Atlas dashboard and outputs so you can follow along visually.
Creating a Cluster
Create a free MongoDB Atlas account
Schema
Create a cluster and a database called collegeDB
Inside it, create a collection called students
Schema
Each student document follows this structure:
*Create *(Insert)
Insert at least 5 student records:
{
student_id: "S001",
name: "Jamie",
age: 19,
department: "CSBS",
year: 2,
cgpa: 8.5
}
{
student_id: "S002",
name: "David",
age: 21,
department: "CSE",
year: 3,
cgpa: 9
}
{
student_id: "S003",
name: "Natalia",
age: 18,
department: "ME",
year: 3,
cgpa: 8.7
}
{
student_id: "S004",
name: "Olivia",
age: 22,
department: "ECE",
year: 4,
cgpa: 8.5
}
{
student_id: "S005",
name: "Jonathan",
age: 20,
department: "IT",
year: 3,
cgpa: 7.9
}
Read (Query):-
Display all student records.
Fetch all students: {}
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:-
Update the CGPA of a specific student.
{ "student_id": "S005" }
Increase the year of study for all 3rd year students by 1.
{ "year": 3 }
Delete:-
Delete one student record by student_id.
{ "student_id": "S004" }
Delete all students having CGPA < 7.5
{ "cgpa": { "$lt": 7.5 } }
🚀 Wrap Up
In this tutorial, we learned how to:
Insert new student records
Query students by CGPA and department
Update CGPA and year of study
Delete students based on conditions
MongoDB CRUD operations form the foundation for building real-world apps like college portals, e-learning systems, or admin dashboards.
Thanks to @santhoshnc sir for guiding and motivating us.
Top comments (0)