
- 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 documents from a collection in MongoDB?
To delete the documents from a collection in MongoDB, you need to use remove() method. The syntax is as follows:
db.yourCollectionName.remove(yourDeleteValue);
Here, let us create a collection with some documents. The query is as follows:
>db.deleteDocuments.insert({"UserId":1,"UserName":"Bob","UserTechnicalSubject":"Introducti on to PL/SQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":2,"UserName":"Carol","UserTechnicalSubject":"Introduction to MongoDB"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":3,"UserName":"John","UserTechnicalSubject":"Introduction to MySQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":4,"UserName":"Maxwell","UserTechnicalSubject":"Introduction to SQL Server"}); WriteResult({ "nInserted" : 1 })
You can display all documents from the collection we created above with the help of find() command. The query is as follows:
> db.deleteDocuments.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6aaa9e64f3d70fcc9147ff"), "UserId" : 1, "UserName" : "Bob", "UserTechnicalSubject" : "Introduction to PL/SQL" } { "_id" : ObjectId("5c6aaab464f3d70fcc914800"), "UserId" : 2, "UserName" : "Carol", "UserTechnicalSubject" : "Introduction to MongoDB" } { "_id" : ObjectId("5c6aaac764f3d70fcc914801"), "UserId" : 3, "UserName" : "John", "UserTechnicalSubject" : "Introduction to MySQL" } { "_id" : ObjectId("5c6aaadc64f3d70fcc914802"), "UserId" : 4, "UserName" : "Maxwell", "UserTechnicalSubject" : "Introduction to SQL Server" }
Delete a single document
To delete a single document from a collection, you need to use remove() method. The query is as follows. Let’s say we are deleting document with “UserId:4”:
>db.deleteDocuments.remove({"UserId":4,"UserName":"Maxwell","UserTechnicalSubject":"Int roduction to SQL Server"}); WriteResult({ "nRemoved" : 2 })
Now you can display all the documents with the help of find() command to verify that “UserId:4” document is deleted successfully or not. The query is as follows:
> db.deleteDocuments.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6aaa9e64f3d70fcc9147ff"), "UserId" : 1, "UserName" : "Bob", "UserTechnicalSubject" : "Introduction to PL/SQL" } { "_id" : ObjectId("5c6aaab464f3d70fcc914800"), "UserId" : 2, "UserName" : "Carol", "UserTechnicalSubject" : "Introduction to MongoDB" } { "_id" : ObjectId("5c6aaac764f3d70fcc914801"), "UserId" : 3, "UserName" : "John", "UserTechnicalSubject" : "Introduction to MySQL" }
Delete all the documents
To delete all the documents, you need to use the following syntax:
db.yourCollectionName.remove({ });
The query is as follows:
> db.deleteDocuments.remove({});
The following is the output:
WriteResult({ "nRemoved" : 3 })
We have deleted all the documents from the collection ‘deleteDocuments’:
- Related Articles
- How to delete all the documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- How to delete multiple documents from a collection using Java?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How to retrieve all the documents from a MongoDB collection using Java?
- How to remove all documents from a collection except a single document in MongoDB?
- Delete a collection from MongoDB with special characters?
- Fetching all documents from MongoDB Collection in a beautified form
- Find a value in lowercase from a MongoDB collection with documents
- Retrieving specific documents from collection by _id in MongoDB
- Delete data from a collection in MongoDB using multiple conditions?
- How to delete multiple documents in MongoDB using deleteMany()?
- How to count the number of documents in a MongoDB collection?
- How to Update multiple documents in a MongoDB collection using Java?
- How to return documents of a collection without objectId in MongoDB?
