DEV Community

Cover image for CRUD OPERATIONS IN MONGODB
Sugesh
Sugesh

Posted on

CRUD OPERATIONS IN MONGODB

Introduction:-

MongoDB is one of the most popular NoSQL databases used by developers today for building modern, scalable applications. Unlike traditional relational databases, MongoDB stores data in flexible JSON-like documents, making it easier to work with real-world scenarios.

Outcome:-

How to insert multiple documents into a collection

How to read and filter records using queries

How to update documents (single & multiple)

How to delete documents based on conditions

How CRUD fits into real-world development

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: "S002",
name: "Jaswant",
age: 20,
department: "CSE",
year: 1,
cgpa: 9
}

{
student_id: "S001",
name: "jai surya",
age: 23,
department: "CSE",
year: 3,
cgpa: 9
}

{
student_id: "S001",
name: "sugesh",
age: 21,
department: "CSBS",
year: 2,
cgpa: 8
}

{
student_id: "S004",
name: "prian",
age: 24,
department: "CSE",
year: 3,
cgpa: 9
}

{
student_id: "S005",
name: "raksanth",
age: 23,
department: "CSBS",
year: 4,
cgpa: 9
}

Read (Query):-

Display all student records.

Fetch all students: {}

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:

Increase the year of study for all 3rd year students by 1.

{ "year": 3 }

Delete:-

Delete one student record by student_id.

{ "student_id": "S004" }

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.

This step-by-step approach gave me a solid understanding of how a database schema works in real-world academic systems

Thanks to @santhoshnc sir for guiding and motivating us.

Top comments (0)