
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to remove document on the basis of _id in MongoDB?
To remove document on the basis of _id in MongoDB, implement the following syntax
db.yourCollectionName.remove({“_id”:ObjectId(“yourId”});
Let us first implement the following query to create a collection with documents
>db.removeDocumentOnBasisOfId.insertOne({"UserName":"Larry","UserAge":23,"UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c986f9f330fd0aa0d2fe4a3") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Sam","UserAge":21,"UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c986fb4330fd0aa0d2fe4a4") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Chris","UserAge":24,"UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c986fc0330fd0aa0d2fe4a5") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Robert","UserAge":26,"UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c986fcf330fd0aa0d2fe4a6") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"David","UserAge":28,"UserCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c986fed330fd0aa0d2fe4a7") }
Following is the query to display all documents from a collection with the help of find() method
> db.removeDocumentOnBasisOfId.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c986f9f330fd0aa0d2fe4a3"), "UserName" : "Larry", "UserAge" : 23, "UserCountryName" : "US" } { "_id" : ObjectId("5c986fb4330fd0aa0d2fe4a4"), "UserName" : "Sam", "UserAge" : 21, "UserCountryName" : "UK" } { "_id" : ObjectId("5c986fc0330fd0aa0d2fe4a5"), "UserName" : "Chris", "UserAge" : 24, "UserCountryName" : "US" } { "_id" : ObjectId("5c986fcf330fd0aa0d2fe4a6"), "UserName" : "Robert", "UserAge" : 26, "UserCountryName" : "UK" } { "_id" : ObjectId("5c986fed330fd0aa0d2fe4a7"), "UserName" : "David", "UserAge" : 28, "UserCountryName" : "AUS" }
Following is the query to remove document on the basis of _id
> db.removeDocumentOnBasisOfId.remove({"_id":ObjectId("5c986fc0330fd0aa0d2fe4a5")}); WriteResult({ "nRemoved" : 1 })
We have removed the records of Chris on the basis of its _id above. Now display all documents from the collection to check the document has been removed or not
> db.removeDocumentOnBasisOfId.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c986f9f330fd0aa0d2fe4a3"), "UserName" : "Larry", "UserAge" : 23, "UserCountryName" : "US" } { "_id" : ObjectId("5c986fb4330fd0aa0d2fe4a4"), "UserName" : "Sam", "UserAge" : 21, "UserCountryName" : "UK" } { "_id" : ObjectId("5c986fcf330fd0aa0d2fe4a6"), "UserName" : "Robert", "UserAge" : 26, "UserCountryName" : "UK" } { "_id" : ObjectId("5c986fed330fd0aa0d2fe4a7"), "UserName" : "David", "UserAge" : 28, "UserCountryName" : "AUS" }
- Related Articles
- Remove only one document in MongoDB?
- How to prevent MongoDB from returning the object ID while finding a document?
- MongoDB query to remove a specific document
- MongoDB query to remove subdocument from document?
- How to remove a field completely from a MongoDB document?
- Remove only a single document in MongoDB
- Update a MongoDB document with Student Id and Name
- How to use or operator in MongoDB to fetch records on the basis of existence?
- MongoDB query to remove array elements from a document?
- MongoDB query to count records on the basis of matching criteria
- Remove values from a matrix like document in MongoDB
- How to perform $gt on a hash in a MongoDB document?
- How to remove an element from a doubly-nested array in a MongoDB document?
- How to remove all documents from a collection except a single document in MongoDB?
- MongoDB query to find on the basis of true or false values

Advertisements