
- 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
MongoDB query to $pull / $unset with multiple conditions?
For this, use $pull along with update. Let us create a collection with documents −
> db.demo198.insertOne({"List":{"Values":[10,20,30,30,70,80,90]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c224503d395bdc21346df") } > db.demo198.insertOne({"List":{"Values":[56,978,56,34,23,34]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c225403d395bdc21346e0") } > db.demo198.insertOne({"List":{"Values":[21,12,14,15,34,56]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c226603d395bdc21346e1") }
Display all documents from a collection with the help of find() method −
> db.demo198.find();
This will produce the following output −
{ "_id" : ObjectId("5e3c224503d395bdc21346df"), "List" : { "Values" : [ 10, 20, 30, 30, 70, 80, 90 ] } } { "_id" : ObjectId("5e3c225403d395bdc21346e0"), "List" : { "Values" : [ 56, 978, 56, 34, 23, 34 ] } } { "_id" : ObjectId("5e3c226603d395bdc21346e1"), "List" : { "Values" : [ 21, 12, 14, 15, 34, 56 ] } }
Following is the query to $pull / $unset with multiple conditions −
> db.demo198.update({},{ "$pull": { "List.Values": { "$lt": 40 } } },{ "multi": true }); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
Display all documents from a collection with the help of find() method −
> db.demo198.find();
This will produce the following output −
{ "_id" : ObjectId("5e3c224503d395bdc21346df"), "List" : { "Values" : [ 70, 80, 90 ] } } { "_id" : ObjectId("5e3c225403d395bdc21346e0"), "List" : { "Values" : [ 56, 978, 56 ] } } { "_id" : ObjectId("5e3c226603d395bdc21346e1"), "List" : { "Values" : [ 56 ] } }
- Related Articles
- MongoDB query to pull multiple values from array
- MongoDB query to get documents with multiple conditions set in $or?
- How to update array with multiple conditions in MongoDB
- Implement multiple conditions in MongoDB?
- MongoDB query to pull array element from a collection?
- Pull multiple objects from an array in MongoDB?
- MongoDB query to pull a specific value from a document
- MongoDB pull with positional operator?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- MongoDB query to add multiple documents
- MongoDB multiple OR conditions on same key?
- How to unset objects in MongoDB?
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to find value in array with multiple criteria (range)
- Query for multiple parameters in MongoDB?

Advertisements