
- 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
Delete data from a collection in MongoDB using multiple conditions?
You can use remove() for this. Let us first create a collection with documents −
> db.deleteDataDemo.insertOne({_id:1,"Name":"Larry"}); { "acknowledged" : true, "insertedId" : 1 } > db.deleteDataDemo.insertOne({_id:2,"Name":"Chris"}); { "acknowledged" : true, "insertedId" : 2 } > db.deleteDataDemo.insertOne({_id:3,"Name":"Robert"}); { "acknowledged" : true, "insertedId" : 3 } > db.deleteDataDemo.insertOne({_id:4,"Name":"David"}); { "acknowledged" : true, "insertedId" : 4 } > db.deleteDataDemo.insertOne({_id:5,"Name":"Carol"}); { "acknowledged" : true, "insertedId" : 5 } > db.deleteDataDemo.insertOne({_id:6,"Name":"Sam"}); { "acknowledged" : true, "insertedId" : 6 }
Following is the query to display all documents from a collection with the help of find() method −
> db.deleteDataDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "Name" : "Larry" } { "_id" : 2, "Name" : "Chris" } { "_id" : 3, "Name" : "Robert" } { "_id" : 4, "Name" : "David" } { "_id" : 5, "Name" : "Carol" } { "_id" : 6, "Name" : "Sam" }
Following is the query to delete data from a collection in MongoDB by using multiple conditions. Here we have used two conditions i.e. _id 4 and Name David −
> db.deleteDataDemo.remove({'_id':4,'Name':"David"}); WriteResult({ "nRemoved" : 1 })
Let us check all the documents once again −
> db.deleteDataDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "Name" : "Larry" } { "_id" : 2, "Name" : "Chris" } { "_id" : 3, "Name" : "Robert" } { "_id" : 5, "Name" : "Carol" } { "_id" : 6, "Name" : "Sam" }
- Related Articles
- How to delete multiple documents from a collection using Java?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How to delete documents from a collection in MongoDB?
- Delete a collection from MongoDB with special characters?
- Get the size of a collection in MongoDB using conditions?
- Retrieve data from a MongoDB collection?
- How to delete all the documents from a collection in MongoDB?
- Implement multiple conditions in MongoDB?
- How to delete multiple documents in MongoDB using deleteMany()?
- How to Update multiple documents in a MongoDB collection using Java?
- MongoDB query to remove entire data from a collection
- How to insert multiple document into a MongoDB collection using Java?
- Delete partial data in MongoDB?
- How to delete multiple ids in MongoDB?
- MongoDB multiple OR conditions on same key?

Advertisements