DEV Community

Deepana
Deepana

Posted on

πŸ”Ή CRUD Operations in MongoDB – A Hands-on College Student Example


Enter fullscreen mode Exit fullscreen mode

πŸ“˜ Objective

In this post, we’ll explore how to perform CRUD (Create, Read, Update, Delete) operations in MongoDB using a simple College Student schema.
This activity helped me understand how MongoDB stores data in collections and how to interact with it using queries.

🧩 Schema – Collection: students

Each document in the collection follows this structure:

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

βš™οΈ 1️⃣ Create (Insert)

Insert multiple student records:

db.students.insertMany([
  { "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
  { "student_id": "S002", "name": "Deepa", "age": 19, "department": "CSE", "year": 1, "cgpa": 8.7 },
  { "student_id": "S003", "name": "Meena", "age": 21, "department": "IT", "year": 3, "cgpa": 7.9 },
  { "student_id": "S004", "name": "Kavin", "age": 22, "department": "CSBS", "year": 3, "cgpa": 8.4 },
  { "student_id": "S005", "name": "Arun", "age": 20, "department": "EEE", "year": 2, "cgpa": 6.9 }
])

Enter fullscreen mode Exit fullscreen mode

πŸ” 2️⃣ Read (Query)

πŸ“Œ Display all student records:

db.students.find()
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Find students with CGPA > 8:

db.students.find({ cgpa: { $gt: 8 } })
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Find students belonging to the Computer Science

db.students.find({ department: { $regex: "CS", $options: "i" } })

Enter fullscreen mode Exit fullscreen mode

✏️ 3️⃣ Update

πŸ“Œ Update the CGPA of a specific student:

db.students.updateOne({ student_id: "S002" }, { $set: { cgpa: 9.1 } })
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Increase the year of study for all 3rd year students by 1:

db.students.updateMany({ year: 3 }, { $inc: { year: 1 } })
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ 4️⃣ Delete

πŸ“Œ Delete one student record by student_id:

db.students.deleteOne({ student_id: "S005" })
Enter fullscreen mode Exit fullscreen mode


πŸ“Œ Delete all students having CGPA < 7.5:

db.students.deleteMany({ cgpa: { $lt: 7.5 } })
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Deliverables

βœ… MongoDB queries (insert, find, update, delete)
βœ… Screenshots from MongoDB Atlas showing query outputs
βœ… Export of the final students collection in JSON/CSV format

πŸ’‘ Summary

Through this exercise, I learned how MongoDB handles data in a document-based structure and how CRUD operations can be done easily using built-in commands.

πŸ™ Special Thanks

A special thanks to Santhosh NC Sir for his guidance and continuous support in this DBMS learning journey.

🏷️ Tags

mongodb #dbms #crud #students #database #learning

Top comments (0)