Remove document whose value is matched with $eq from a MongoDB collection?

To remove documents from a MongoDB collection using the $eq operator, use the remove() method with a query that matches field values. The $eq operator explicitly matches documents where a field equals the specified value.

Syntax

db.collection.remove({
    fieldName: { $eq: "value" }
});

Create Sample Data

Let us create a collection with documents −

db.demo626.insertMany([
    {id: 1, "Name": "Chris"},
    {id: 2, "Name": "David"},
    {id: 3, "Name": "Bob"},
    {id: 4, "Name": "Mike"}
]);
{
   "acknowledged": true,
   "insertedIds": [
      ObjectId("5e9ac6376c954c74be91e6ae"),
      ObjectId("5e9ac63e6c954c74be91e6af"),
      ObjectId("5e9ac6436c954c74be91e6b0"),
      ObjectId("5e9ac6486c954c74be91e6b1")
   ]
}

Display Current Documents

db.demo626.find();
{ "_id": ObjectId("5e9ac6376c954c74be91e6ae"), "id": 1, "Name": "Chris" }
{ "_id": ObjectId("5e9ac63e6c954c74be91e6af"), "id": 2, "Name": "David" }
{ "_id": ObjectId("5e9ac6436c954c74be91e6b0"), "id": 3, "Name": "Bob" }
{ "_id": ObjectId("5e9ac6486c954c74be91e6b1"), "id": 4, "Name": "Mike" }

Remove Document Using $eq

Remove the document where Name equals "Bob" −

db.demo626.remove({Name: {$eq: "Bob"}});
WriteResult({ "nRemoved": 1 })

Verify Removal

db.demo626.find();
{ "_id": ObjectId("5e9ac6376c954c74be91e6ae"), "id": 1, "Name": "Chris" }
{ "_id": ObjectId("5e9ac63e6c954c74be91e6af"), "id": 2, "Name": "David" }
{ "_id": ObjectId("5e9ac6486c954c74be91e6b1"), "id": 4, "Name": "Mike" }

Key Points

  • The $eq operator provides explicit equality matching, equivalent to {Name: "Bob"}
  • remove() is deprecated − use deleteOne() or deleteMany() in modern applications
  • The method returns a WriteResult showing the number of removed documents

Conclusion

The $eq operator with remove() effectively deletes documents matching exact field values. While functional, prefer deleteOne() or deleteMany() for new applications as remove() is deprecated.

Updated on: 2026-03-15T03:12:08+05:30

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements