
- 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
Pull multiple objects from an array in MongoDB?
To pull multiple objects from an array, you can use $pull operator. Let us first create a collection with documents −
> db.pullMultipleObjectsDemo.insertOne( ... { ... "ClientId" : "100", ... "ClientName" : "John", ... "ClientPersonalDetails" : [ ... { ... "ClientCountryName" : "US", ... "ClientProjectName" : "Online Book Store", ... ... }, ... { ... "ClientCountryName" : "AUS", ... "ClientProjectName" : "Online Fee Management", ... ... }, ... { ... "ClientCountryName" : "UK", ... "ClientProjectName" : "Online Pig Dice Game", ... ... }, ... { ... "ClientCountryName" : "ANGOLA", ... "ClientProjectName" : "Online Hospital Management", ... ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc7d0748f9e6ff3eb0ce43d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.pullMultipleObjectsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7d0748f9e6ff3eb0ce43d"), "ClientId" : "100", "ClientName" : "John", "ClientPersonalDetails" : [ { "ClientCountryName" : "US", "ClientProjectName" : "Online Book Store" }, { "ClientCountryName" : "AUS", "ClientProjectName" : "Online Fee Management" }, { "ClientCountryName" : "UK", "ClientProjectName" : "Online Pig Dice Game" }, { "ClientCountryName" : "ANGOLA", "ClientProjectName" : "Online Hospital Management" } ] }
Following is the query to pull multiple objects from an array −
> db.pullMultipleObjectsDemo.update( ... {"_id": ObjectId("5cc7d0748f9e6ff3eb0ce43d")}, ... {"$pull":{"ClientPersonalDetails":{"ClientProjectName":{$in:["Online Book Store","Online Pig Dice Game"]}}}} ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us display all documents from the collection in order to check the objects have been removed from an array or not. The query is as follows −
> db.pullMultipleObjectsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7d0748f9e6ff3eb0ce43d"), "ClientId" : "100", "ClientName" : "John", "ClientPersonalDetails" : [ { "ClientCountryName" : "AUS", "ClientProjectName" : "Online Fee Management" }, { "ClientCountryName" : "ANGOLA", "ClientProjectName" : "Online Hospital Management" } ] }
Look at the above sample output, the multiple objects have been removed from the array.
- Related Articles
- MongoDB query to pull multiple values from array
- How to pull even numbers from an array in MongoDB?
- Removing an array element from MongoDB collection using update() and $pull
- How to pull all elements from an array in MongoDB without any condition?
- Pull an element in sub of sub-array in MongoDB?
- How to pull value from array of ObjectIDs in MongoDB?
- MongoDB query to pull array element from a collection?
- MongoDB query to $pull / $unset with multiple conditions?
- MongoDB query to find multiple matchings inside array of objects?
- How to pull an array element (which is a document) in MongoDB?
- Update multiple elements in an array in MongoDB?
- How to use MongoDB $pull to delete documents within an Array?
- How to pull distinct values from an array in java?
- Make an array of multiple arrays in MongoDB?
- Can we search an array of objects in MongoDB?

Advertisements