Paramclasses Academy - Blog

Tutorials Details

←
Post 10 of 35
→

MongoDB CRUD Operations Explained: Create, Read, Update and Delete

MongoDB CRUD Operations Explained: Create, Read, Update and Delete

November 5, 2026

MongoDB CRUD Operations Explained: Create, Read, Update and Delete

❖ Welcome to Param Classes!

When working with a database, applications need to store information, retrieve records, modify existing data, and remove unwanted records. MongoDB provides a group of operations that allow developers to perform these tasks easily.

These operations are known as CRUD operations.

CRUD stands for:

  • C – Create: Insert new documents into a collection.

  • R – Read: Retrieve documents from a collection.

  • U – Update: Modify existing documents.

  • D – Delete: Remove documents from a collection.

In this beginner-friendly tutorial by Param Classes, you will learn all four MongoDB CRUD operations using practical examples and MongoDB Shell commands.

We will use a student management database to understand how CRUD operations work in a real-world situation.

✦ 1. What Are CRUD Operations in MongoDB?

CRUD is the basic process of managing data in a database.

For example, imagine you are developing a student management system. You need to:

  1. Add a new student's information.

  2. Display the list of registered students.

  3. Change a student's course or mobile number.

  4. Remove a student record when required.

MongoDB provides methods to perform each of these operations.

CRUD Operation MongoDB Method Purpose
Create insertOne(), insertMany() Add documents
Read find(), findOne() Retrieve documents
Update updateOne(), updateMany() Modify documents
Delete deleteOne(), deleteMany() Remove documents

✦ 2. Prepare Your MongoDB Database

Before performing CRUD operations, make sure MongoDB Server is running and you have connected through MongoDB Shell (mongosh) or another MongoDB interface.

Step 1: Open MongoDB Shell.

Step 2: Select a database named college.

use college

Step 3: Create a collection named students and insert a sample document.

db.students.insertOne({
    name: "Rahul",
    age: 21,
    course: "Web Development",
    city: "Raipur",
    status: "Active"
})

MongoDB creates the students collection automatically if it does not already exist.

You now have a database and a collection to practise CRUD operations.

✦ 3. CREATE Operation in MongoDB

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

MongoDB provides two commonly used methods:

  • insertOne()

  • insertMany()

A. insertOne() Method

The insertOne() method inserts a single document into a collection.

Syntax:

db.collectionName.insertOne({
    field1: "value1",
    field2: "value2"
})

Example:

db.students.insertOne({
    name: "Amit",
    age: 22,
    course: "Python",
    city: "Raipur",
    status: "Active"
})

Expected Output:

{
    acknowledged: true,
    insertedId: ObjectId("...")
}

The acknowledged field indicates that MongoDB acknowledged the operation. The insertedId contains the unique ID assigned to the new document.

B. insertMany() Method

The insertMany() method inserts multiple documents into a collection in one operation.

Syntax:

db.collectionName.insertMany([
    { field1: "value1" },
    { field1: "value2" }
])

Example:

db.students.insertMany([
    {
        name: "Priya",
        age: 20,
        course: "Java",
        city: "Raipur",
        status: "Active"
    },
    {
        name: "Rohit",
        age: 23,
        course: "PHP",
        city: "Bilaspur",
        status: "Active"
    },
    {
        name: "Neha",
        age: 21,
        course: "JavaScript",
        city: "Durg",
        status: "Inactive"
    }
])

Expected Output:

{
    acknowledged: true,
    insertedIds: {
        '0': ObjectId("..."),
        '1': ObjectId("..."),
        '2': ObjectId("...")
    }
}

MongoDB returns the IDs of the inserted documents.

When should you use insertOne() and insertMany()?

  • Use insertOne() when adding one record.

  • Use insertMany() when adding multiple records.

✦ 4. READ Operation in MongoDB

The Read operation is used to retrieve documents from a collection.

MongoDB provides two important methods:

  • find()

  • findOne()

A. find() Method

The find() method retrieves all documents or documents matching a specified condition.

Syntax:

db.collectionName.find()

Example:

db.students.find()

This command displays all documents stored in the students collection.

B. findOne() Method

The findOne() method returns a single matching document.

Example:

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

This command searches for a student whose name is Rahul and returns one matching document.

C. Find Documents Using Conditions

You can also retrieve documents using specific conditions.

Example 1: Find students from Raipur

db.students.find({
    city: "Raipur"
})

Example 2: Find students enrolled in Java

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

Example 3: Find students whose age is greater than 21

db.students.find({
    age: { $gt: 21 }
})

Here, $gt means greater than.

D. Find Documents Using Multiple Conditions

You can provide multiple fields in a query to match documents that satisfy all specified conditions.

Example:

db.students.find({
    city: "Raipur",
    status: "Active"
})

This query finds students whose city is Raipur and whose status is Active.

✦ 5. UPDATE Operation in MongoDB

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

MongoDB provides these commonly used methods:

  • updateOne()

  • updateMany()

A. updateOne() Method

The updateOne() method updates the first document that matches the specified filter.

Syntax:

db.collectionName.updateOne(
    { condition },
    { $set: { field: "new value" } }
)

Example: Update a student's course

db.students.updateOne(
    { name: "Rahul" },
    { $set: { course: "Full Stack Development" } }
)

This command updates Rahul's course.

Expected Output:

{
    acknowledged: true,
    matchedCount: 1,
    modifiedCount: 1
}
  • matchedCount: Number of documents matching the filter.

  • modifiedCount: Number of documents actually modified.

B. updateMany() Method

The updateMany() method updates all documents that match the specified filter.

Example: Update the status of all Raipur students

db.students.updateMany(
    { city: "Raipur" },
    { $set: { status: "Verified" } }
)

This command changes the status of every matching student from Raipur to Verified.

C. Update Multiple Fields

You can update more than one field in a document using $set.

Example:

db.students.updateOne(
    { name: "Amit" },
    {
        $set: {
            age: 23,
            course: "Advanced Python",
            status: "Active"
        }
    }
)

This updates Amit's age, course, and status.

Important: Always use a proper filter when updating documents. If you use an empty filter with updateMany(), all documents in the collection may be updated.

✦ 6. DELETE Operation in MongoDB

The Delete operation is used to remove documents from a collection.

MongoDB provides two important methods:

  • deleteOne()

  • deleteMany()

A. deleteOne() Method

The deleteOne() method deletes the first document that matches the specified filter.

Syntax:

db.collectionName.deleteOne({
    condition
})

Example:

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

This command deletes one matching student document.

Expected Output:

{
    acknowledged: true,
    deletedCount: 1
}

The deletedCount value indicates how many documents were deleted.

B. deleteMany() Method

The deleteMany() method deletes all documents that match the specified filter.

Example:

db.students.deleteMany({
    status: "Inactive"
})

This command deletes all student documents whose status is Inactive.

Important: Be careful while using deleteMany(). Always check your filter with find() before deleting records.

For example:

db.students.find({
    status: "Inactive"
})

After verifying the matching documents, you can run the delete command.

✦ 7. Difference Between MongoDB CRUD Methods

Let's understand the difference between the main CRUD methods.

Operation Method Description
Create insertOne() Inserts one document
Create insertMany() Inserts multiple documents
Read find() Retrieves matching documents
Read findOne() Retrieves one matching document
Update updateOne() Updates one matching document
Update updateMany() Updates all matching documents
Delete deleteOne() Deletes one matching document
Delete deleteMany() Deletes all matching documents

✦ 8. Complete Practical Example: Student Management System

Now let's perform all four CRUD operations in one practical example.

Step 1: Select the Database

use college

Step 2: Insert Student Records

db.students.insertMany([
    {
        name: "Ankit",
        age: 21,
        course: "MongoDB",
        city: "Raipur",
        status: "Active"
    },
    {
        name: "Sneha",
        age: 22,
        course: "Node.js",
        city: "Durg",
        status: "Active"
    },
    {
        name: "Vikas",
        age: 20,
        course: "Python",
        city: "Bilaspur",
        status: "Inactive"
    }
])

Step 3: Display All Students

db.students.find()

Step 4: Search for a Particular Student

db.students.findOne({
    name: "Ankit"
})

Step 5: Update Student Information

db.students.updateOne(
    { name: "Ankit" },
    { $set: { course: "Full Stack Development" } }
)

Step 6: Verify Updated Information

db.students.find({
    name: "Ankit"
})

Step 7: Delete a Student Record

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

Step 8: Display the Remaining Records

db.students.find()

Congratulations! You have successfully performed Create, Read, Update, and Delete operations in MongoDB.

✦ 9. Common Mistakes While Performing CRUD Operations

Beginners sometimes face errors or unexpected results while working with MongoDB.

Here are some common mistakes and how to avoid them.

1. Collection Name Mistake

MongoDB collection names must match the names used in your commands.

For example:

db.students.find()

If your actual collection is named student, using students will query a different collection.

2. Incorrect Field Name

Field names are case-sensitive.

For example, name and Name are different fields.

3. Forgetting the $set Operator

When updating specific fields, use $set.

db.students.updateOne(
    { name: "Rahul" },
    { $set: { age: 24 } }
)

4. Using an Incorrect Filter

An incorrect filter may update or delete unexpected records. Check the matching documents using find() before running update or delete commands.

5. MongoDB Server Is Not Running

If you cannot connect to MongoDB Shell, check whether your MongoDB Server is running and whether you are using the correct connection details.

✦ 10. Practice Task: Manage Your Own Student Database

Now it is time to practise what you have learned.

Create a database named college and a collection named students.

Insert at least five student documents with these fields:

  • name

  • age

  • course

  • city

  • status

Complete the following tasks:

  1. Insert one student using insertOne().

  2. Insert multiple students using insertMany().

  3. Display all students using find().

  4. Find a student by name using findOne().

  5. Find all students from Raipur.

  6. Update one student's course using updateOne().

  7. Update the status of multiple students using updateMany().

  8. Delete one student using deleteOne().

  9. Delete inactive student records using deleteMany() after checking the matching records.

Try to complete these tasks yourself before looking at the examples again.

✦ Conclusion

CRUD operations are the foundation of database management in MongoDB. They allow developers to insert new data, retrieve existing records, update information, and delete unwanted documents.

In this tutorial, you learned how to use:

  • insertOne() and insertMany() for creating documents.

  • find() and findOne() for reading documents.

  • updateOne() and updateMany() for updating documents.

  • deleteOne() and deleteMany() for deleting documents.

Understanding these operations is essential for building real-world applications such as student management systems, blogs, e-commerce websites, and user management platforms.

At Param Classes, we believe that practical learning makes programming easier. Keep practising MongoDB commands and move forward to advanced database concepts.

Learn Today, Grow Tomorrow — Param Classes.

←
Post 10 of 35
→