
- 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 delete document by _id using MongoDB?
To delete by _id, use remove() in MongoDB. Following is the syntax −
db.yourCollectionName.remove({_id:yourObjectId});
To understand the above syntax, let us create a collection with documents −
> db.demo518.insertOne({"ClientName":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b02db3fbf26334ef610e") } > db.demo518.insertOne({"ClientName":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b030b3fbf26334ef610f") } > db.demo518.insertOne({"ClientName":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b035b3fbf26334ef6110") }
Display all documents from a collection with the help of find() method −
> db.demo518.find();
This will produce the following output −
{ "_id" : ObjectId("5e88b02db3fbf26334ef610e"), "ClientName" : "Chris" } { "_id" : ObjectId("5e88b030b3fbf26334ef610f"), "ClientName" : "Bob" } { "_id" : ObjectId("5e88b035b3fbf26334ef6110"), "ClientName" : "David" }
Following is the query to delete by _id −
> db.demo518.remove({_id:ObjectId("5e88b02db3fbf26334ef610e")}); WriteResult({ "nRemoved" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo518.find();
This will produce the following output −
{ "_id" : ObjectId("5e88b030b3fbf26334ef610f"), "ClientName" : "Bob" } { "_id" : ObjectId("5e88b035b3fbf26334ef6110"), "ClientName" : "David" }
- Related Articles
- How to delete a MongoDB document using Java?
- How to delete a document in MongoDB?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How to delete particular data in a document in MongoDB?
- How to find by id in MongoDB?
- How to prevent MongoDB from returning the object ID while finding a document?
- How to search document in MongoDB by _id
- Update a MongoDB document with Student Id and Name
- How do I delete array value from a document in MongoDB?
- MongoDB query to exclude if id is equal to a document field array value
- How do I get email-id from a MongoDB document and display with print()
- How to delete multiple documents in MongoDB using deleteMany()?
- Matching MongoDB collection items by id?
- How to update an existing document in MongoDB collection using Java?
- How to insert a document into a MongoDB collection using Java?

Advertisements