- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove document whose value is matched with $eq from a MongoDB collection?
Remove document using remove(), whose value is matched with $eq from a MongoDB collection. The $eq operator matches documents where the value of a field equals the specified value.
Let us create a collection with documents −
> db.demo626.insertOne({id:1,"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac6376c954c74be91e6ae") } > db.demo626.insertOne({id:2,"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac63e6c954c74be91e6af") } > db.demo626.insertOne({id:3,"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac6436c954c74be91e6b0") } > db.demo626.insertOne({id:4,"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac6486c954c74be91e6b1") }
Display all documents from a collection with the help of find() method −
> db.demo626.find();
This will produce the following output −
{ "_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" }
Following is the query to remove document from collection −
> db.demo626.remove({Name:{$eq:"Bob"}}); WriteResult({ "nRemoved" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo626.find();
This will produce the following output −
{ "_id" : ObjectId("5e9ac6376c954c74be91e6ae"), "id" : 1, "Name" : "Chris" } { "_id" : ObjectId("5e9ac63e6c954c74be91e6af"), "id" : 2, "Name" : "David" } { "_id" : ObjectId("5e9ac6486c954c74be91e6b1"), "id" : 4, "Name" : "Mike" }
- Related Articles
- How to remove all documents from a collection except a single document in MongoDB?
- Get the top most document from a MongoDB collection
- MongoDB query to update a specific document from a collection
- MongoDB query to remove subdocument from document?
- MongoDB query to remove array elements from a document?
- Remove values from a matrix like document in MongoDB
- Find a value in lowercase from a MongoDB collection with documents
- How to remove duplicates from MongoDB Collection?
- MongoDB query to remove entire data from a collection
- How to remove a field completely from a MongoDB document?
- MongoDB query to remove entire array from collection?
- How to delete document from a collection in MongoDB using deleteOne() method?
- Retrieving the first document in a MongoDB collection?
- MongoDB query to pull a specific value from a document
- Remove only a single document in MongoDB

Advertisements