- 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
MongoDB query to match and remove element from an array?
To match and remove element(s) , use MongoDB $pullAll. Let us first create a collection with documents −
> db.removeElementsDemo.insertOne({"ListOfNames":["Mike","Sam","David","Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e071e5a25ddae1f53b62203") }
Following is the query to display all documents from a collection with the help of find() method −
> db.removeElementsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e071e5a25ddae1f53b62203"), "ListOfNames" : [ "Mike", "Sam", "David", "Carol" ] }
Here is the query to match and remove element(s) from an array −
> db.removeElementsDemo.update( ... { }, ... { ... $pullAll: ... { ... "ListOfNames": ["Carol"] ... } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Following is the query to display all documents from a collection with the help of find() method −
> db.removeElementsDemo.find().pretty();
This will produce the following output. Above, we removed only a single value −
{ "_id" : ObjectId("5e071e5a25ddae1f53b62203"), "ListOfNames" : [ "Mike", "Sam", "David" ] }
- Related Articles
- MongoDB query to remove element from array as sub property
- MongoDB query to remove item from array?
- MongoDB query to remove entire array from collection?
- Remove null element from MongoDB array?
- MongoDB query to match documents that contain an array field
- MongoDB query to remove array elements from a document?
- MongoDB query to match each element in a documents array to a condition?
- MongoDB query to get record beginning with specific element from an array?
- MongoDB query to pull array element from a collection?
- MongoDB query to remove empty objects in an object-array?
- Match element in array of MongoDB?
- How to remove a specific element from array in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?
- MongoDB query to insert an array element with a condition?
- MongoDB query to remove subdocument from document?

Advertisements