$pullAll Operator in MongoDB: Easy Guide

PullAll Operator Featured Image

In this blog, I am going to discuss the $pullAll operator in MongoDB.

One of the most popular database management services available today is MongoDB, which includes a wide range of advanced tools. No matter how smart a developer may be, it is never easy to memorize all its operators and features. Sure, it is possible, but it is much less frequently done.

An application that uses such a powerful database system can include a wide range of features and functionalities. Furthermore, enhanced security functions can be implemented. The purpose of today’s article is to explain how to use the $pullAll operator in MongoDB to help remove elements from arrays.

A wide variety of operators and features are available in MongoDB, depending on what you need. A MongoDB database allows you to build applications that are capable of doing a lot more than CRUD functions. A MongoDB database is designed especially for building applications. The popularity of MongoDB stems from its flexibility and power.

In addition to supporting querying, printing, and changing data, the database also offers a variety of operators. Regardless of the level of experience which its users possess, MongoDB has something to offer.

Today, I want to cover the $pullAll operator for you all. The $pullAll operator is an operator that works on arrays. We can use this operator to remove all instances of a specified or a given value from the given array field.

The $pullAll operator is almost similar to the $pull operator. It can be used with all update operation functions like update(), findAndModify() and more.

In this guide, I will demonstrate a few examples to explain how we can use the $pullAll operator in MongoDb in our projects or applications.

The $pullAll Operator Syntax in MongoDB

Let a give a glance at the $pullAll operator syntax in MongoDB.

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

Using the $pullAll Operator in MongoDB

Let us start to use the $pullAll operator with different examples to understand its abilities.

Removing Elements from Array Fields Using the $pullAll Operator

In this example, we will use the $pullAll operator to remove elements or items from an array field in MongoDB.

  • Run the MongoDB service
  • Decide on what database you wish to use and navigate into it:
show dbs
use droneVerse
  • Before we move ahead, let us take a quick look at all our documents in the drones collection:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : [
                "Monitoring or Inspection",
                "Photography"
        ],
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : [
                "Security",
                "Photography"
        ],
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection",
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : [
                "Monitoring or Inspection",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : [
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security",
                        "Combat"
                ]
        }
}

So, this is what our documents look like.

  • Next, let’s say I decided to remove a few elements inside the utility array field for all documents. This is where we will use the $pullAll operator in MongoDB:
> db.drones.updateMany({}, {$pullAll: {utility: ["Monitoring or Inspection", "Recreation", "Security"]}})

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
  • Checking what our documenst now look like after using the $pullAll operation:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : [ ],
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : [
                "Photography"
        ],
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : [
                "Photography"
        ],
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection",
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : [ ],
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : [ ],
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security",
                        "Combat"
                ]
        }
}

Perfect! You can notice that most of our documents are now left blank and some are left with values that we did not wish to remove using the $pullAll operator.

Removing Elements from Nested Array Fields Using the $pullAll Operator

In this example, we will now get rid of a few elements in the nested array fields with the use of the $pullAll operator in MongoDB.

Here’s how we can go about it:

> db.drones.updateMany({}, {$pullAll: {"additionalDetails.moreUses": ["Land Inspection", "Combat", "Water Inspection"]}})

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 4 }
  • Checking our documents to see how they look like after applying the $pullAll operator:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : [ ],
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : [
                "Photography"
        ],
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : [
                "Photography"
        ],
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : [ ],
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [ ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : [ ],
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security"
                ]
        }
}

Amazing! The nested array fields of some of the documents are now empty and some of them do not contain the elements we specified using the $pullAll operator in MongoDB.

Read More: $pull Operator in MongoDB

Conclusion

Learning to use the $pullAll operator in MongoDB with different examples.

Noteworthy References

MongoDB Docs