DEV Community

Cover image for CRUD Operation in MongoDB
Harish T
Harish T

Posted on

CRUD Operation in MongoDB

Create (Insert):-

Insert at least 5 student records into the students collection.

We cannot start by inserting 5 student records into our students collection together. We can create each student as separate document.

Code:-

{
student_id: "S001",
name: "Harshitha",
age: 20,
department: "CSBS",
year: 2,
cgpa: 9
}


{
student_id: "S002",
name: "Madhummitha",
age: 21,
department: "CSBS",
year: 3,
cgpa: 8.5
}


{
student_id: "S003",
name: "Lokhitha",
age: 20,
department: "MI",
year: 3,
cgpa: 8
}

{
student_id: "S004",
name: "Hareesh",
age: 22,
department: "IT",
year: 3,
cgpa: 9.2
}


{
student_id: "S005",
name: "Dhaaraneesh",
age: 20,
department: "CIVIL",
year: 2,
cgpa: 8.8
}
{
student_id: "S006",
name: "Harish",
age: 22,
department: "CSE",
year: 3,
cgpa: 9.2
}

READ(QUERY)

Find all students with CGPA > 8.

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

Find students belonging to the Computer Science department.

db.students.find({ department: "CSE" })

UPDATE

Update the CGPA of a specific student.

{ "student_id": "S005" }


Delete:-

Delete one student record by student_id.

{ "student_id": "S004" }

RESULT

Conclusion:-

In this blog, we explored how to perform CRUD operations in MongoDB using a real-world example of a student database. Starting from inserting records, querying based on conditions, updating multiple documents, and finally deleting specific records – we’ve covered the foundation of working with MongoDB.

CRUD operations form the building blocks of every application, whether you’re managing users in a website, products in an e-commerce app, or students in a college system.

Thanks to Mr.@santhoshnc for encouraging us in exploring new paths in technology and gain knowledge.

Top comments (0)