In MongoDB, remove() is used to delete documents from a collection.

Note: remove() is an older MongoDB method. In modern MongoDB, use deleteOne() or deleteMany().

1. Remove One Document

Suppose:

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

This removes a matching document.


2. Remove Multiple Documents — multi: true

db.students.remove(
    { city: "Rajkot" },
    { multi: true }
)

This removes all documents where:

city = Rajkot

For example:

{ name: "Rahul", city: "Rajkot" }
{ name: "Amit", city: "Rajkot" }
{ name: "Ravi", city: "Ahmedabad" }

After:

db.students.remove(
    { city: "Rajkot" },
    { multi: true }
)

Remaining:

{ name: "Ravi", city: "Ahmedabad" }

3. Remove Using Age

db.students.remove(
    { age: { $gt: 25 } },
    { multi: true }
)

This removes all students whose age is greater than 25.


4. Remove a Specific Document Using _id

db.students.remove(
    { _id: ObjectId("64abc123456789abcdef1234") }
)

This removes the document having that specific _id.


5. Remove All Documents

⚠️ Be careful:

db.students.remove({})

An empty {} matches all documents, so this can delete the entire collection’s documents.

Modern equivalent:

db.students.deleteMany({})

remove() vs Modern Methods

Old MethodModern MethodPurpose
remove()deleteOne()Delete one document
remove() + multi:truedeleteMany()Delete multiple documents

Easy to Remember

// Old MongoDB
db.students.remove({ city: "Rajkot" }, { multi: true })

// Modern MongoDB
db.students.deleteMany({ city: "Rajkot" })

So, multi: true in remove() means delete all documents matching the condition.

If you mean remove a field from a MongoDB document, use the $unset operator.

To remove the country field:

db.students.updateOne(
    { name: "Rahul" },
    { $unset: { country: "" } }
)

Remove Multiple Fields

db.students.updateOne(
    { name: "Rahul" },
    {
        $unset: {
            country: "",
            age: ""
        }
    }
)

db.students.deleteOne({ name: “Rahul” })

By admin