The Wayback Machine - https://web.archive.org/web/20240204103616/https://www.geeksforgeeks.org/mongodb-pullall-operator/
Open In App
Related Articles

MongoDB – $pullAll Operator

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

MongoDB provides different types of array update operators to update the values of the array fields in the documents and $pullAll operator is one of them. This operator is used to remove all instances of the specified values from an existing array. It is different from $pull operator, $pull operator removes items by specifying a query, whereas $pullAll operator removes items that matches the listed values. You can use this operator with methods like update(), findAndModify(), etc., according to your requirement.

Syntax:

{ $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }

Here, field can specify with dot notation in embedded/nested documents or an array and if value is an array or a document, then $pullAll operator will remove only those items in the array that match the specified value.

Note: The $pullAll operator will remove items from the array in the same order in which they are specified in the value.

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: two documents that contain the details of the contributor in the form of field-value pairs.

Image

Removing items from an array using $pullAll operator:

In this example, we are removing items specified in the list, i.e., [“Java”, “C#”, “Python”] from the language field with the help of $pullAll operator.




db.contributor.update({name: "Rohit"},
                      {$pullAll: {language: ["Java", "C#", "Python"]}})


Image

Removing items from an array in the embedded document using $pullAll operator:

In this example, we are removing items specified in the list, i.e., [71, 72] from the personal.semesterMarks field with the help of $pullAll operator.




db.contributor.update({name: "Sumit"},
                      {$pullAll: {"personal.semesterMarks": [71, 72]}})


Image

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 10 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads