DEV Community

NAHULESWARAN S 24CB031
NAHULESWARAN S 24CB031

Posted on

CRUD Operations in MongoDB — Student Management System

🎯 Objective

To gain hands-on experience performing CRUD (Create, Read, Update, Delete) operations in MongoDB Atlas using a simple college student schema.

MongoDB is a NoSQL document database that stores data in a flexible, JSON-like format — making it ideal for dynamic and structured data such as student records.

🧱 Schema Design

We’ll use a collection called students.
Each student document follows this structure:

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

⚙️Step 1: Create (Insert)

Insert at least 5 student records into the students collection.

MongoDB Query:
db.students.insertMany

{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
},
{
"student_id": "S002",
"name": "Priya",
"age": 19,
"department": "CSE",
"year": 1,
"cgpa": 8.7
},
{
"student_id": "S003",
"name": "Karthik",
"age": 21,
"department": "IT",
"year": 3,
"cgpa": 7.4
},
{
"student_id": "S004",
"name": "Meena",
"age": 22,
"department": "ECE",
"year": 4,
"cgpa": 8.1
},
{
"student_id": "S005",
"name": "Arun",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9.2
}


✅ Result in Atlas:
Inserted 5 documents successfully.

🔍 Step 2: Read (Query)

a. Display all student records
db.students.find();

b. Find all students with CGPA > 8
db.students.find({ cgpa: { $gt: 8 } });

c. Find students belonging to the Computer Science department
db.students.find({ department: { $in: ["CSE", "CSBS"] } });


✅ Tip:
Use the MongoDB Atlas “Filter” option to test these queries easily.

✏️ Step 3: Update

a. Update the CGPA of a specific student
Example: Update Santhosh’s CGPA to 9.5
db.students.updateOne(
{ student_id: "S001" },
{ $set: { cgpa: 9.5 } }
);


Modified 1 document.

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


✅ Output:
Shows how many documents were modified.


Step 4: Delete

a. Delete one student record by student_id
Example: Delete student S003
db.students.deleteOne({ student_id: "S003" });


b. Delete all students having CGPA < 7.5
db.students.deleteMany({ cgpa: { $lt: 7.5 } });

✅ Output:
Displays number of deleted documents.


📤Step 5: Export the Collection

After performing all CRUD operations, you can export your collection from MongoDB Atlas:

🪶 Steps:

Go to your cluster → Collections → Select students.
Click Export Collection.
Choose JSON or CSV format.
Download your file.
📸 Deliverables

✅ MongoDB Queries (as shown above)
✅ Screenshots of query results (from Atlas)
✅ Exported students.json or students.csv file

🌟** Conclusion**
This hands-on lab demonstrates how easily MongoDB handles CRUD operations using a flexible schema.
The students collection can be expanded further to include additional attributes like address, email, or course list — giving you real-world database experience for college or project work.

Top comments (0)