🚀 Exploring MongoDB CRUD Operations: A Beginner-Friendly Walkthrough
MongoDB is one of the most widely used NoSQL databases, known for its scalability, flexibility, and document-based structure. In this tutorial, we’ll explore how to perform the core CRUD operations (Create, Read, Update, Delete) on a college students collection to understand MongoDB’s power in real-world use cases.
🛠️ Setting Up the Cluster Create a free account on MongoDB Atlas. Build your first cluster using the default free-tier configuration. Connect it with MongoDB Compass or VS Code for seamless query execution.
🧩 Schema Design
Each student document in the collection follows this structure:
{ student_id: "A101", name: "Aarav", age: 20, department: "AIML", year: 2, cgpa: 8.6 }
This document model makes it easy to store and query data without worrying about rigid table structures.
✳️ CREATE (Insert Documents)
Let’s begin by inserting a few student records into our collection:
{ student_id: "A101", name: "Aarav", age: 20, department: "AIML", year: 2, cgpa: 8.6 } { student_id: "A102", name: "Diya", age: 21, department: "CSE", year: 3, cgpa: 9.2 } { student_id: "A103", name: "Karthik", age: 19, department: "EEE", year: 2, cgpa: 8.3 } { student_id: "A104", name: "Meera", age: 22, department: "ECE", year: 4, cgpa: 7.8 } { student_id: "A105", name: "Rahul", age: 20, department: "IT", year: 3, cgpa: 8.9 }
Each record represents a student with essential details like department, academic year, and CGPA.
🔍 READ (Query Documents)
To fetch all student records, you can simply use:
db.students.find({})
This command retrieves all documents from the students collection. You can also apply filters, for example, to display only students with a CGPA above 8.5:
db.students.find({ cgpa: { $gt: 8.5 } })
These queries make MongoDB powerful and intuitive for working with large datasets.
✏️ UPDATE (Modify Documents)
Now, let’s perform some update operations.
1️⃣ Update a specific student’s CGPA:
db.students.updateOne( { student_id: "A105" }, { $set: { cgpa: 9.1 } } )
2️⃣ Increase the year of study for all 3rd-year students by 1:
db.students.updateMany( { year: 3 }, { $inc: { year: 1 } } )
MongoDB allows both single document and bulk updates, making it ideal for large-scale applications like college management systems.
🗑️ DELETE (Remove Documents)
To remove a specific record by student ID:
db.students.deleteOne({ student_id: "A104" })
To delete all students whose CGPA is below 7.5:
db.students.deleteMany({ cgpa: { $lt: 7.5 } })
Deletion operations in MongoDB are straightforward yet powerful—ensuring that your database remains clean and efficient.
🎓 Wrap-Up
In this walkthrough, we learned how to:
Insert new student records Retrieve data based on filters like department and CGPA Update values dynamically Delete specific or conditional records
These CRUD operations form the backbone of every MongoDB application, whether it’s a college portal, an e-learning platform, or a student management dashboard.
A huge thanks to @santhoshnc sir for his continuous guidance and encouragement throughout this learning journey.
Top comments (0)