

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- MongoDB query to pull array element from a collection?
- Pull an element in sub of sub-array in MongoDB?
- How to pull value from array of ObjectIDs in MongoDB?
- MongoDB query to $pull / $unset with multiple conditions?
- How to pull distinct values from an array in java?
- How to use MongoDB $pull to delete documents within an Array?
- 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?
- Make an array of multiple arrays in MongoDB?
- Can we search an array of objects in MongoDB?
Advertisements