DEV Community

Pranav Aadithya
Pranav Aadithya

Posted on

CRUD Operations in MongoDB: Hands-On with a Student Collection

MongoDB is a NoSQL document database that stores data in JSON-like documents. In this tutorial, we’ll perform CRUD operations using a simple college student schema.

1️⃣ Create (Insert)
Schema:
Student 1
{
"student_id": "S001",
"name": "Pranav",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}
Student 2
{
"student_id": "S002",
"name": "Anitha",
"age": 21,
"department": "CSE",
"year": 3,
"cgpa": 8.5
}
Student 3
{
"student_id": "S003",
"name": "Ramesh",
"age": 22,
"department": "ECE",
"year": 4,
"cgpa": 7.2
}
Student 4
{
"student_id": "S004",
"name": "Priya",
"age": 19,
"department": "CSBS",
"year": 1,
"cgpa": 9.3
}
Student 5
{
"student_id": "S005",
"name": "Vignesh",
"age": 20,
"department": "Mechanical",
"year": 2,
"cgpa": 6.8
}

Screenshot:

2️⃣Read (Query)

A.Display all students
{}

B.Students with CGPA > 8

{ "cgpa": { "$gt": 8 } }

C.Students in CSBS department

{ "department": { "$in": ["CSBS"] } }

3️⃣Update

A.Update CGPA of a specific student (S002)
{ "student_id": "S002" }

B. Increase year of all 3rd year students by 1
{ "year": 3 }

4️⃣Delete

A. Delete one student by student_id (S005)

{ "student_id": "S004" }

B. Delete all students with CGPA < 7.5

{ "cgpa": { "$lt": 7.5 } }

✅ Summary of MongoDB CRUD Operations
Operation MongoDB Query
Create insertOne / insertMany
Read find()
Update updateOne / updateMany
Delete deleteOne / deleteMany

MongoDB allows flexible, schema-less data storage, making it ideal for applications requiring rapid iteration and scalability.

Top comments (0)