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

MongoDB – $pullAll Operator

Last Updated : 24 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

MongoDB $pullAll operator is a crucial tool for efficiently managing arrays within MongoDB documents. It allows users to remove all instances of specified values from an array and provides a direct way to update array fields. In this article, We will learn about the MongoDB $pullAll Operator by understanding various examples in detail.

MongoDB $pullAll Operator

  • The MongoDB $pullAll operator is used to remove all instances of the specified values from an array.
  • 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 the $pull operator.
  • $pull operator removes items by specifying a query, whereas $pullAll operator removes items that match the listed values. We can use this operator with methods like update(), findAndModify(), etc. according to our requirements.

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.

Examples of MongoDB $pullAll

We will use the contributor collection on which we will perform various examples are shown below:

Example 1: Removing items from an array

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"]}})

Output:

Example 2: Removing items from an array in the embedded document

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]}})

Output:

pullall-example-2

Conclusion

The $pullAll operator in MongoDB provides a convenient way to remove multiple specified values from an array field in documents. Its straightforward syntax and ability to remove items directly based on their values make it a valuable tool for array manipulation in MongoDB updates.

FAQs on MongoDB $pullAll

Can $pullAll remove items from multiple arrays in the same document?

Yes, $pullAll can be used to remove items from multiple arrays by specifying each array field and its corresponding values to remove.

What happens if a specified value to remove is not found in the array?

If a specified value is not found in the array, $pullAll has no effect on that particular value, and the array remains unchanged.

Can $pullAll be used with nested arrays?

Yes, $pullAll can be used with nested arrays by specifying the nested field path in the operator.

Does the order of values in the $pullAll operator matter?

Yes, the order of values in the $pullAll operator matters. Items are removed from the array in the same order as they are specified in the value list.


Previous Article
Next Article

Similar Reads

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass
Pre-requisite: AWS and MongoDB In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass.  If you haven't installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and
4 min read
How to Integrate MongoDB Atlas and Segment using MongoDB Stitch
Data integration has become a crucial component of modern business strategies, allowing companies to enhance the power of data for informed decision-making. According to a recent survey, 85% of businesses believe that integrating data across multiple platforms is important for their success. In this article, We will learn about What is MongoDB Stit
5 min read
MongoDB - Delete Multiple Documents Using MongoDB Shell
The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter. The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method
2 min read
Connect MongoDB Atlas Cluster to MongoDB Compass
MongoDB Compass is a free GUI for MongoDB. You might want to connect MongoDB Atlas Cluster to MongoDB Compass to take benefit of the GUI model for database administration. This guide explains how to connect MongoDB Atlas Cluster to MongoDB Compass. The connection process involves obtaining a connection string from MongoDB Atlas, configuring MongoDB
4 min read
MongoDB Shell vs MongoDB Node.JS Driver
MongoDB Shell and the MongoDB Node.js Driver are both tools for interacting with MongoDB, but they serve different purposes and are used in different contexts. Here's a comparison of the two: MongoDB Node.JS DriverA Node.js package called the MongoDB Node.js Driver enables programmatic communication between applications and MongoDB databases. Throu
4 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database known for flexibility and scalability. MongoDB Compass offers a graphical interface for managing databases visually while MongoDB Atlas is a cloud-based service for automated management and scaling of MongoDB databases. In this article, We will learn about the in-depth understanding of the difference between the
5 min read
MongoDB AND operator ( $and )
MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array. You can use this operator in methods like find(), update(), etc. ac
2 min read
MongoDB NOT operator ( $not )
MongoDB provides different types of logical query operators and $not operator is one of them. This operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression. It also includes those documents that don't contain the field. You can
2 min read
MongoDB OR operator ( $or )
MongoDB provides different types of logical query operators and $or operator is one of them. This operator is used to perform logical OR operation on the array of two or more expressions and select or retrieve only those documents that match at least one of the given expression in the array. You can use this operator in methods like find(), update(
2 min read
MongoDB NOR operator ( $nor )
MongoDB provides different types of logical query operators and $nor operator is one of them. This operator is used to perform logical NOR operation on the array of one or more expressions and select or retrieve only those documents that do not match all the given expression in the array. You can use this operator in methods like find(), update(),
2 min read
MongoDB - Current Date Operator ($currentDate)
MongoDB provides different types of field update operators to update the values of the fields of the documents and $currentDate operator is one of them. This operator is used to set the value of a field to the current date (either as a timestamp or as a Date). The default type of $currentDate operator is a Date. This operator can also work with emb
2 min read
MongoDB $ln Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $ln operator is one of them. This operator is used to find the natural logarithm of a number and returns the result as a double. Syntax: { $ln: <number> } Here, the number is a valid expression until it resolves to a non- n
1 min read
MongoDB $sqrt Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages $sqrt operator is one of them. This operator is used to find the square root of a positive number and returns the result as a double. Syntax: { $sqrt: <number> } Here, the number is a valid expression until it resolves to a non
2 min read
MongoDB $mod Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $mod operator is one of them. This operator is used to divide one number by another number and return the remainder. Syntax: { $mod: [ <expression1>, <expression2> ] } Here, in this operator, the arguments passed in a
1 min read
MongoDB - $ne Operator
MongoDB $ne or "not equals" operator is one of the comparison operators. The $ne operator selects those documents where the field value is not equal to the given value. It also includes those documents that do not contain the specified field. You can use this operator in methods like find(), update(), etc. as per your requirement. Syntax{field: {$n
2 min read
MongoDB - $nin Operator
MongoDB $nin or " not in" is one of the comparison query operators. The $nin operator selects those documents where the field value is not equal to any of the given values in the array and the field that does not exist. You can use this operator in methods like find(), update(), etc. according to your requirements. Syntax{field: {$nin: [value1, val
2 min read
MongoDB - $max Operator
MongoDB $max or maximum operator is one of the field update operators. $max operator updates the field with the specified value if the specified value is greater than the current value. Important Points This operator will compare the values of different data types according to the BSON comparison order.You can also use this operator in embedded/nes
3 min read
MongoDB - $min operator
MongoDB $min or minimum operator is one of the field update operators. $min operator updates the field with the specified value if the specified value is less than the current value. The $min operator will compare the values of different data types according to the BSON comparison order. This operator can also be used in embedded/nested documents u
3 min read
MongoDB - $inc Operator
MongoDB $inc or increment operator is a type of field update operator. $inc operator increases the values of the fields to the specified amount or increases the field by the given value. Important PointsThis operator accepts positive and negative values.If the given field does not exist, then this operator will create a field and set the value of t
2 min read
MongoDB - $lt Operator
MongoDB $lt operator or less than operator is one of the comparison operators. The $lt operator selects the documents with a value less than (<) the given value. You can use this operator in methods like, find(), update(), etc. as per your requirements. Syntax{field: {$lt: value}}MongoDB $lt Operator ExampleIn the following examples, we are work
2 min read
MongoDB - $lte Operator
MongoDB $lte Operator is one of the comparison operators. $lte operator selects those documents where the field value is less than equal to (<=) the given value. This operator can be used in methods like find(), update(), etc. according to your requirements.. Syntax{field: {$lte: value}}MongoDB $lte Operator ExampleIn the following examples, we
2 min read
How to use the $lookup operator in aggregation pipelines in MongoDB?
One of the essential stages of the Aggregation Pipeline is the $lookup. It enables us to accomplish a left outer join between two collections. This step is quite beneficial when we have to compose data from different collections into a single document. The Aggregation Pipeline in MongoDB is a robust tool that allows us to manipulate and analyze dat
4 min read
MongoDB $gt Operator
MongoDB $gt operator selects documents where the value of a field is greater than a specified value. $gt Operator in MongoDBThe $gt operator in MongoDB is a comparison operator. It is used to find documents where the value in the field is greater than(>) a given value. This operator can be used with other methods like find, update, etc. accordin
2 min read
MongoDB - $gte Operator
MongoDB $gte or "greater than equals to" operator is one of the comparison operators. $gte operator selects those documents where the field value is greater than equals to(>=) the given value. This operator can be used in methods (like, find(), update(), etc.) according to your requirements. Syntax{field: {$gte: value}}MongoDB $gte Operator Exam
2 min read
MongoDB - $eq Operator
MongoDB $eq operator or equality operator is a comparison operator. The $eq operator matches documents where the value of the field is equal to the specified value. Important Points: If the given value is a document, then the order of the fields in the document is important.If the given value is an array, then MongoDB matches the documents where th
2 min read
MongoDB - $concat Operator
The $concat operator in MongoDB is a useful tool for string manipulation within aggregation pipelines. It allows us to concatenate strings or expressions to create new fields or modify existing ones. In this article, We will learn about the $concat operator in MongoDB by understanding various examples in detail. MongoDB - $concat OperatorThe $conca
3 min read
MongoDB $subtract Operator
MongoDB $subtract operator is a powerful tool in the aggregation pipeline that allows users to perform subtraction operations on numbers or dates. In this article, We will learn about MongoDB $subtract Operator by understanding various examples in detail. MongoDB $subtract OperatorMongoDB provides different types of arithmetic expression operators
3 min read
MongoDB $addToSet Operator
The $addToSet operator in MongoDB allows us to add elements to an array field if they are not already present and ensure that the array contains unique values. In this article, We will learn about the MongoDB $addToSet Operator by understanding various examples in detail. $addToSet Operator in MongoDBThe $addToSet operator in MongoDB is used to add
3 min read
MongoDB - Rename Operator ($rename)
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure. This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in
4 min read
MongoDB $arrayElemAt Operator
MongoDB $arrayElemAt operator is a key component of the aggregation framework, allowing users to retrieve specific elements from arrays within their documents. In this article, We will learn about the MongoDB $arrayElemAt by understanding various examples in detail and so on. MongoDB $arrayElemAtMongoDB provides different types of array expression
3 min read