DEV Community

Cover image for MongoDB CRUD Operations
Gangeswara
Gangeswara

Posted on

MongoDB CRUD Operations

Introduction

In this assignment, I explored MongoDB CRUD operations to gain hands-on experience with a NoSQL database. The main objective was to create, read, update, and delete student records in a simple college student schema.

MongoDB is a document-oriented database that stores data in JSON-like documents, making it flexible for dynamic data structures compared to traditional relational databases.

*Create Cluster & Database
*

Before performing CRUD operations, we need to create a cluster and define the database:

Create Cluster

In MongoDB Atlas, click “Create Deployment” → select Cluster.

Choose a cloud provider and region.

Enter Cluster Name: CollegeCluster

Click Create Deployment.


**
Create Database & Collection**

After the cluster is ready, go to Browse Collections → Add My Own Data.

Enter Database Name: college

Enter Collection Name: students

Click Create.

Student Schema

Each student document follows this structure:

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

student_id → Unique identifier for each student

name → Student’s name

age → Student’s age

department → Department name

year → Current academic year

cgpa → Current grade point average

1️⃣ Create (Insert)

We inserted 5 student records into the students collection using the insertMany command:

db.students.insertMany([
  { student_id: "S001", name: "Santhosh", age: 20, department: "CSBS", year: 2, cgpa: 9 },
  { student_id: "S002", name: "Anitha",  age: 19, department: "CSE",  year: 1, cgpa: 8.2 },
  { student_id: "S003", name: "Karthik",  age: 21, department: "ECE",  year: 3, cgpa: 7.8 },
  { student_id: "S004", name: "Meena",    age: 22, department: "IT",   year: 3, cgpa: 6.9 },
  { student_id: "S005", name: "Vikram",   age: 20, department: "CSBS", year: 2, cgpa: 8.9 }
]);
Enter fullscreen mode Exit fullscreen mode

Explanation: insertMany allows inserting multiple documents at once into a MongoDB collection.



2️⃣ Read (Query)
2.1 Display all student records

db.students.find().pretty();

Enter fullscreen mode Exit fullscreen mode

find() → fetches all documents in the collection

pretty() → formats output for readability

2.2 Students with CGPA > 8

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

$gt → MongoDB operator for “greater than”

Only students with CGPA greater than 8 are retrieved



2.3 Students in CSBS or CSE department

db.students.find({ department: { $in: ["CSBS","CSE"] } }).pretty();

Enter fullscreen mode Exit fullscreen mode

$in → matches any value in the given array

Retrieves students belonging to either CSBS or CSE


3️⃣ Update

3.1 Update CGPA of a specific student

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

Enter fullscreen mode Exit fullscreen mode

updateOne → updates a single document

$set → sets a new value for the specified field


3.2 Increase year for all 3rd-year students

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

Enter fullscreen mode Exit fullscreen mode

updateMany → updates multiple documents matching the filter

$inc → increments numeric field by a specified value




4️⃣ Delete
4.1 Delete one student by ID

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

deleteOne → deletes a single document matching the filter


4.2 Delete all students with CGPA < 7.5

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

Enter fullscreen mode Exit fullscreen mode

deleteMany → deletes all documents matching the filter

$lt → “less than” operator


Final Collection Sample

[
  { "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
  { "student_id": "S002", "name": "Anitha",  "age": 19, "department": "CSE",  "year": 1, "cgpa": 8.5 },
  { "student_id": "S005", "name": "Vikram",   "age": 20, "department": "CSBS", "year": 2, "cgpa": 8.9 }
]
Enter fullscreen mode Exit fullscreen mode

Conclusion

Through this assignment, I gained practical experience with MongoDB CRUD operations: inserting, querying, updating, deleting, and exporting data.
It highlighted the flexibility of document-oriented databases for managing dynamic data, compared to traditional relational databases.

Thanks to @santhoshnc Sir for his guidance and support throughout this assignment. 🙏

mongodb #crud #nosql #students #dbms #atlas #assignment #learningbydoing

Top comments (0)