- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pull all elements from an array in MongoDB without any condition?
You can use $set operator for this. Let us first create a collection with documents −
> db.pullAllElementDemo.insertOne( ... { ... "StudentId":101, ... "StudentDetails" : [ ... { ... ... "StudentName": "Carol", ... "StudentAge":21, ... "StudentCountryName":"US" ... }, ... { ... "StudentName": "Chris", ... "StudentAge":24, ... "StudentCountryName":"AUS" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ccdd9c8685b30d09a7111e4") } > db.pullAllElementDemo.insertOne( ... { ... "StudentId":102, ... "StudentDetails" : [ ... { ... ... "StudentName": "Robert", ... "StudentAge":27, ... "StudentCountryName":"UK" ... }, ... { ... "StudentName": "David", ... "StudentAge":23, ... "StudentCountryName":"US" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ccdd9f7685b30d09a7111e5") }
Following is the query to display all documents from a collection with the help of find() method −
> db.pullAllElementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccdd9c8685b30d09a7111e4"), "StudentId" : 101, "StudentDetails" : [ { "StudentName" : "Carol", "StudentAge" : 21, "StudentCountryName" : "US" }, { "StudentName" : "Chris", "StudentAge" : 24, "StudentCountryName" : "AUS" } ] } { "_id" : ObjectId("5ccdd9f7685b30d09a7111e5"), "StudentId" : 102, "StudentDetails" : [ { "StudentName" : "Robert", "StudentAge" : 27, "StudentCountryName" : "UK" }, { "StudentName" : "David", "StudentAge" : 23, "StudentCountryName" : "US" } ] }
Following is the query to pull all elements from array in MongoDB without any condition. Here, we have removed StudentDetails with StudentId 102 using $set −
> db.pullAllElementDemo.update( {StudentId:102}, { "$set": { "StudentDetails": [] }} ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let is display all documents from the above collection to check those specific elements from an array have been pulled out or not −
> db.pullAllElementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccdd9c8685b30d09a7111e4"), "StudentId" : 101, "StudentDetails" : [ { "StudentName" : "Carol", "StudentAge" : 21, "StudentCountryName" : "US" }, { "StudentName" : "Chris", "StudentAge" : 24, "StudentCountryName" : "AUS" } ] } { "_id" : ObjectId("5ccdd9f7685b30d09a7111e5"), "StudentId" : 102, "StudentDetails" : [ ] }
- Related Articles
- How to pull even numbers from an array in MongoDB?
- Pull multiple objects from an array in MongoDB?
- MongoDB query to filter object where all elements from nested array match the condition
- How to pull value from array of ObjectIDs in MongoDB?
- MongoDB query to pull multiple values from array
- How to use MongoDB $pull to delete documents within an Array?
- Removing an array element from MongoDB collection using update() and $pull
- How to pull an array element (which is a document) in MongoDB?
- MongoDB query to pull array element from a collection?
- How to pull distinct values from an array in java?
- Delete all elements in an array field in MongoDB?
- How to filter an array from all elements of another array – JavaScript?
- Pull an element in sub of sub-array in MongoDB?
- Golang program to remove all elements from an array
- MongoDB query to insert an array element with a condition?

Advertisements