CRUD OPERATIONS IN MONGODB

CRUD OPERATIONS IN MONGODB

Publish Date: Oct 3
2 0

INTRODUCTION

MongoDB is one of the most popular NoSQL databases, widely used for its flexibility and scalability. Unlike relational databases, MongoDB stores data in collections as JSON-like documents.

In this blog, we’ll perform CRUD (Create, Read, Update, Delete) operations on a simple college student schema to understand MongoDB basics hands-on.

Here we are using MongoDB Shell for scripting in MongoDB Compass
run all the commands in MongoDB Shell. this will work great.


Student Schema
We’ll use a collection called students, and each document follows this basic structure:

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

Create (Insert)

Inserting 5 student records:

db.students.insertMany([
  {
    "student_id": "S001",
    "name": "Santhosh",
    "age": 20,
    "department": "CSBS",
    "year": 2,
    "cgpa": 9
  },
  {
    "student_id": "S002",
    "name": "Kavya",
    "age": 21,
    "department": "CSE",
    "year": 3,
    "cgpa": 8.5
  },
  {
    "student_id": "S003",
    "name": "Arun",
    "age": 19,
    "department": "ECE",
    "year": 1,
    "cgpa": 7.8
  },
  {
    "student_id": "S004",
    "name": "Priya",
    "age": 22,
    "department": "CSE",
    "year": 4,
    "cgpa": 9.2
  },
  {
    "student_id": "S005",
    "name": "Vignesh",
    "age": 20,
    "department": "MECH",
    "year": 2,
    "cgpa": 6.9
  }
])
Enter fullscreen mode Exit fullscreen mode

This will create 5 documents in the students collection.

Read (Query)

Display all student records:

db.students.find()
Enter fullscreen mode Exit fullscreen mode

Find all students with CGPA > 8:

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

Find students belonging to the Computer Science department:

db.students.find({ department: "CSE" })
Enter fullscreen mode Exit fullscreen mode

Update

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

db.students.updateOne(
  { student_id: "S002" },
  { $set: { cgpa: 9.1 } }
)
Enter fullscreen mode Exit fullscreen mode

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

db.students.updateMany(
  { year: 3 },
  { $inc: { year: 1 } }
)
Enter fullscreen mode Exit fullscreen mode

Delete

Delete one student record by student_id:

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

Delete all students having CGPA < 7.5:

db.students.deleteMany({ cgpa: { $lt: 7.5 } })
Enter fullscreen mode Exit fullscreen mode

Deliverables

  • MongoDB queries (provided above)
  • Screenshots of execution results from MongoDB Atlas / Compass (to be added when you run them).
  • Export final students collection as JSON/CSV

Conclusion

We’ve successfully performed CRUD operations in MongoDB using a simple student schema. This hands-on exercise gives a clear picture of how MongoDB works with JSON-like documents.

Comments 0 total

    Add comment