✍️

MongoDB CRUD Operations Tutorial for Beginners with Examples

MongoDB CRUD Operations Complete Beginner Guide with Examples

MongoDB CRUD Operations Complete Beginner Guide

Learn Create, Read, Update, and Delete operations in MongoDB with easy examples, practice questions, and MCQs.

MongoDB CRUD operations create read update delete illustration

MongoDB CRUD Operations are the basic operations used to manage data inside a MongoDB database. CRUD is one of the most important topics for beginners who are learning MongoDB, backend development, or MERN Stack development.

What is CRUD in MongoDB?

CRUD stands for:

Letter Meaning
C Create
R Read
U Update
D Delete

CRUD operations are used to insert, read, update, and delete data from a database. Almost every website and application uses CRUD operations.

Example:
Registration form uses Create operation.
Profile page uses Read operation.
Edit profile uses Update operation.
Delete account uses Delete operation.

Create Operation in MongoDB

Create operation is used to insert new documents into a MongoDB collection.

  • insertOne()
  • insertMany()

Insert One Document

db.students.insertOne({
   name: "Harsh",
   age: 21,
   course: "BCA",
   marks: 85
})

Insert Multiple Documents

db.students.insertMany([
   {
      name: "Rahul",
      age: 20,
      course: "BCA",
      marks: 75
   },
   {
      name: "Aman",
      age: 22,
      course: "BTech",
      marks: 90
   }
])

Read Operation in MongoDB

Read operation is used to fetch or display data from a MongoDB collection.

Show All Documents

db.students.find()

Find One Document

db.students.findOne()

Find Data Using Condition

db.students.find({
   course: "BCA"
})

Update Operation in MongoDB

Update operation is used to modify existing documents in a collection.

Update One Document

db.students.updateOne(
   { name: "Harsh" },
   {
      $set: {
         marks: 95
      }
   }
)

Update Multiple Documents

db.students.updateMany(
   { course: "BCA" },
   {
      $set: {
         status: "Pass"
      }
   }
)

Delete Operation in MongoDB

Delete operation is used to remove documents from a collection.

Delete One Document

db.students.deleteOne({
   name: "Rahul"
})

Delete Multiple Documents

db.students.deleteMany({
   course: "BCA"
})
Important: Always use conditions carefully with updateMany() and deleteMany(). Wrong conditions can update or delete many records.

MongoDB Update Operations and Important Update Operators

In MongoDB, update operations are used when we want to change existing data inside a collection. The two most common update methods are updateOne() and updateMany().

Method Use
updateOne() Updates only the first matching document.
updateMany() Updates all documents that match the condition.

Sample Data for Update Queries

db.students.insertMany([
   {
      name: "Harsh",
      age: 21,
      course: "BCA",
      marks: 85,
      skills: ["HTML", "CSS"]
   },
   {
      name: "Rahul",
      age: 20,
      course: "BCA",
      marks: 70,
      skills: ["JavaScript"]
   },
   {
      name: "Aman",
      age: 22,
      course: "MCA",
      marks: 90,
      skills: ["MongoDB", "Node.js"]
   }
])

1. $set Operator

The $set operator is used to update an existing field value or add a new field if it does not exist.

db.students.updateOne(
   { name: "Harsh" },
   {
      $set: {
         marks: 95,
         city: "Delhi"
      }
   }
)
Meaning: Harsh ke marks 95 ho jayenge aur city field add ho jayegi.

2. $unset Operator

The $unset operator is used to remove a field from a document.

db.students.updateOne(
   { name: "Harsh" },
   {
      $unset: {
         city: ""
      }
   }
)
Meaning: Harsh ke document se city field remove ho jayegi.

3. $inc Operator

The $inc operator is used to increase or decrease a numeric field value.

db.students.updateOne(
   { name: "Rahul" },
   {
      $inc: {
         marks: 5
      }
   }
)
db.students.updateOne(
   { name: "Rahul" },
   {
      $inc: {
         marks: -10
      }
   }
)
Meaning: Positive value se marks increase honge aur negative value se marks decrease honge.

4. $push Operator

The $push operator is used to add a new value inside an array field.

db.students.updateOne(
   { name: "Harsh" },
   {
      $push: {
         skills: "JavaScript"
      }
   }
)
Meaning: Harsh ke skills array me JavaScript add ho jayega.

5. $each Operator with $push

The $each operator is used with $push to add multiple values inside an array at the same time.

db.students.updateOne(
   { name: "Harsh" },
   {
      $push: {
         skills: {
            $each: ["PHP", "MySQL", "React"]
         }
      }
   }
)
Meaning: Ek hi query me multiple skills add ho jayengi.

6. $pop Operator

The $pop operator is used to remove first or last value from an array.

db.students.updateOne(
   { name: "Harsh" },
   {
      $pop: {
         skills: 1
      }
   }
)
db.students.updateOne(
   { name: "Harsh" },
   {
      $pop: {
         skills: -1
      }
   }
)
Meaning: 1 last value remove karta hai aur -1 first value remove karta hai.

7. $pull Operator

The $pull operator is used to remove a specific value from an array.

db.students.updateOne(
   { name: "Harsh" },
   {
      $pull: {
         skills: "CSS"
      }
   }
)

8. $addToSet Operator

The $addToSet operator adds a value to an array only if that value is not already present. It helps to avoid duplicate values.

db.students.updateOne(
   { name: "Harsh" },
   {
      $addToSet: {
         skills: "MongoDB"
      }
   }
)

9. $rename Operator

The $rename operator is used to rename a field name.

db.students.updateMany(
   {},
   {
      $rename: {
         marks: "score"
      }
   }
)

10. Update Many Documents

We can use updateMany() to update all matching documents.

db.students.updateMany(
   { course: "BCA" },
   {
      $set: {
         status: "Pass"
      }
   }
)

Quick Revision Table of MongoDB Update Operators

Operator Use Example Meaning
$setUpdate or add fieldmarks: 95
$unsetRemove fieldRemove city field
$incIncrease or decrease numbermarks +5 or -10
$pushAdd value in arrayAdd JavaScript in skills
$eachAdd multiple array valuesAdd PHP, MySQL, React
$popRemove first or last array value1 = last, -1 = first
$pullRemove specific array valueRemove CSS
$addToSetAdd unique value in arrayAvoid duplicate skills
$renameRename fieldmarks to score

MongoDB CRUD Practice Questions

  1. Insert one student document.
  2. Insert multiple employee documents.
  3. Find all records from a collection.
  4. Find students whose marks are greater than 80.
  5. Use $set to update marks of one student.
  6. Use $unset to remove city field from one document.
  7. Use $inc to increase marks by 10.
  8. Use $inc to decrease age by 1.
  9. Use $push to add one skill in skills array.
  10. Use $each with $push to add multiple skills.
  11. Use $pop to remove last skill from skills array.
  12. Use $pull to remove a specific skill from skills array.
  13. Use $addToSet to add a skill without duplicate entry.
  14. Use updateMany() to add status field to all BCA students.
  15. Delete all records with marks below 40.

MongoDB CRUD MCQ Questions

1. What does CRUD stand for?

2. Which command inserts one document?

3. Which command displays all documents?

4. Which operator updates field value?

5. Which command deletes multiple documents?

6. Which operator is used to increase a numeric value?

7. Which operator removes a field from a document?

8. Which operator adds one value into an array?

9. Which operator is used with $push to add multiple array values?

10. In $pop, which value removes the last item from an array?

Final Conclusion

MongoDB CRUD operations are the foundation of MongoDB. Every beginner should understand how to create, read, update, and delete data properly. These operations are used in almost every website, mobile app, and backend system.

© 2026 MongoDB CRUD Operations Guide. All Rights Reserved.

❮ Previous Next ❯