
- 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 multiple values from array
To pull values, use $pull and set multi: true. Let us first create a collection with documents −
> db.demo392.insertOne( ... { ... Name: 'Chris', ... details: [ ... { ... _id: '101' ... ... }, ... { ... _id: '102' ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5d2b3322064be7ab44e802") } > > db.demo392.insertOne( ... { ... Name: 'Chris', ... details: [ ... { ... _id: '104' ... ... }, ... { ... _id: '101' ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5d2b3422064be7ab44e803") }
Display all documents from a collection with the help of find() method −
> db.demo392.find();
This will produce the following output −
{ "_id" : ObjectId("5e5d2b3322064be7ab44e802"), "Name" : "Chris", "details" : [ { "_id" : "101" }, { "_id" : "102" } ] } { "_id" : ObjectId("5e5d2b3422064be7ab44e803"), "Name" : "Chris", "details" : [ { "_id" : "104" }, { "_id" : "101" } ] }
Following is the query to $pull from array −
> db.demo392.update( ... { }, ... { $pull: { details: { _id: '101' } } }, ... { multi: true } ... ) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo392.find();
This will produce the following output −
{ "_id" : ObjectId("5e5d2b3322064be7ab44e802"), "Name" : "Chris", "details" : [ { "_id" : "102" } ] } { "_id" : ObjectId("5e5d2b3422064be7ab44e803"), "Name" : "Chris", "details" : [ { "_id" : "104" } ] }
- Related Articles
- MongoDB query to pull array element from a collection?
- Pull multiple objects from an array in MongoDB?
- MongoDB query to $pull / $unset with multiple conditions?
- MongoDB query to fetch array values
- MongoDB query to pull a specific value from a document
- Retrieving array values from a find query in MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Retrieving array values from a find query in MongoDB database
- How to pull even numbers from an array in MongoDB?
- How to pull value from array of ObjectIDs in MongoDB?
- How to pull distinct values from an array in java?
- Query to retrieve multiple items in an array in MongoDB?
- MongoDB query to find multiple matchings inside array of objects?
- MongoDB query to remove item from array?
- MongoDB query to find value in array with multiple criteria (range)

Advertisements