DEV Community

Prabavathy Balagurusamy
Prabavathy Balagurusamy

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)