DEV Community

HARI SARAVANAN
HARI SARAVANAN

Posted on

MONGODB IN ACTION : BUILDING A SMART STUDENT DATABASE WITH CRUD OPERATIONS

🧩 Short Introduction (for your blog / LinkedIn post)
πŸ“˜ Introduction

In this task, I explored how to perform CRUD operations (Create, Read, Update, Delete) using MongoDB, a popular NoSQL database that stores data in JSON-like documents.

MongoDB is flexible and schema-less, making it perfect for applications where data structure can evolve over time.
To understand how it works, I created a simple students collection to manage college student records, and performed all CRUD operations step-by-step.
πŸ—οΈ Step 1: Create Collection & Insert Documents
db.students.insertMany([
{ "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9.0 },
{ "student_id": "S002", "name": "Priya", "age": 21, "department": "IT", "year": 3, "cgpa": 8.7 },
{ "student_id": "S003", "name": "Ravi", "age": 22, "department": "ECE", "year": 4, "cgpa": 7.3 },
{ "student_id": "S004", "name": "Meena", "age": 19, "department": "CSBS", "year": 1, "cgpa": 9.2 },
{ "student_id": "S005", "name": "Vikram", "age": 20, "department": "MECH", "year": 2, "cgpa": 8.1 }
]);

πŸ” Step 2: Read (Query) Operations

a) Display all student records

db.students.find();

b) Find all students with CGPA > 8

db.students.find({ cgpa: { $gt: 8 } });

c) Find all students from the "CSBS" department

db.students.find({ department: "CSBS" });

✏️ Step 3: Update Operations

a) Update the CGPA of a specific student (say, S002)

db.students.updateOne(
{ student_id: "S002" },
{ $set: { cgpa: 9.1 } }
);

b) Increase the year of all 3rd year students by 1

db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
);

πŸ—‘οΈ Step 4: Delete Operations

a) Delete one student by ID

db.students.deleteOne({ student_id: "S003" });

b) Delete all students with CGPA < 7.5

db.students.deleteMany({ cgpa: { $lt: 7.5 } });

πŸ“€ Step 5: Export Final Collection

Export as JSON:

mongoexport --uri="your_connection_URI" --collection=students --out=students.json

Export as CSV:

mongoexport --uri="your_connection_URI" --collection=students --type=csv --fields=student_id,name

πŸ’‘ Introduction

(Use the short intro above)

πŸ—οΈ Creating the Database & Collection

(Code + screenshot)

✨ Insert (Create Operation)

(Code + screenshot)

πŸ” Read (Query Operation)

(Code + screenshots for all queries)

✏️ Update Operation

(Code + before/after screenshots)

πŸ—‘οΈ Delete Operation

(Code + before/after screenshots)

πŸ“€ Exporting Data

(Show final JSON/CSV file)

🏁 Conclusion

CRUD operations in MongoDB are simple yet powerful.
They form the foundation of most modern NoSQL-based applications.

Top comments (0)