MongoDB $pull Operator
The $pull operator in MongoDB is a powerful update operator used to remove all instances of a specified value or values from an array. This operator is particularly useful for modifying arrays within documents without retrieving and updating the entire array manually.
In this article, We will learn about the MongoDB $pull Operator by understanding various examples in detail.
MongoDB $pull Operator
-
$pulloperator in MongoDB is used to remove all instances of a specified value or values from an array within a document. - It can also be used for nested arrays, making it a versatile tool.
- If the $pull operator is unable to find the desired value, it returns the original array and makes no changes to it.
- It does not give any error or exceptions, the result will show “{ nModified: 0 }”, indicating that zero elements were modified.
Syntax
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
Here, <field> can specify with dot notation in embedded/nested documents or an array.
Examples of $pull Operator
To understand MongoDB $pull Operator we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called contributor.
{
"_id": 1,
"name": "Alice",
"skills": ["JavaScript", "Python", "Java"]
},
{
"_id": 2,
"name": "Bob",
"skills": ["JavaScript", "Java", "C++"]
},
{
"_id": 3,
"name": "Charlie",
"skills": ["Python", "Ruby", "JavaScript"]
}
Example 1: Removing a Specific Skill
Let’s Remove the skill “Java” from all contributors who have it.
db.contributor.updateMany(
{ skills: "Java" },
{ $pull: { skills: "Java" } }
)
Output:
{
"_id": 1,
"name": "Alice",
"skills": ["JavaScript", "Python"]
},
{
"_id": 2,
"name": "Bob",
"skills": ["JavaScript", "C++"]
},
{
"_id": 3,
"name": "Charlie",
"skills": ["Python", "Ruby", "JavaScript"]
}
Example 2: Removing Multiple Values
Let’s Remove the skills “JavaScript” and “Python” from all contributors who have them.
db.contributor.updateMany(
{ skills: { $in: ["JavaScript", "Python"] } },
{ $pull: { skills: { $in: ["JavaScript", "Python"] } } }
)
Output:
[
{ _id: 1, name: 'Alice', skills: [] },
{ _id: 2, name: 'Bob', skills: [ 'C++' ] },
{ _id: 3, name: 'Charlie', skills: [ 'Ruby' ] }
]
Example 3: Removing Based on Condition
Let’s Remove skills that are shorter than 5 characters from all contributors.
db.contributor.updateMany(
{},
{ $pull: { skills: { $regex: /^.{1,4}$/ } } }
)
Output:
[
{ _id: 1, name: 'Alice', skills: [] },
{ _id: 2, name: 'Bob', skills: [] },
{ _id: 3, name: 'Charlie', skills: [] }
]
Example 4: Removing All Instances of a Value
Let’s Remove all instances of the skill “JavaScript” from the skills array in the contributor collection.
db.contributor.updateMany(
{},
{ $pull: { skills: "JavaScript" } }
)
Output:
[
{ _id: 1, name: 'Alice', skills: [] },
{ _id: 2, name: 'Bob', skills: [] },
{ _id: 3, name: 'Charlie', skills: [] }
]
Key TakeAways About MongoDB $pull Operator
- The $pull operator in MongoDB provides a way to delete specific elements from Arrays and helps in data manipulation and customization.
- In the $pull operator, if you specify the <condition> and the array contains the embedded/nested documents, then this operator applies the <condition> as if each array item were a document in a collection.
- If you specify a <value> in the $pull operator to remove in an array, then this operator will remove only those items in the array that match the specified <value>. Here, the order must be the same.
- If you specify a <value> in the $pull operator to remove a document, then this operator will remove only those items in the array that have the same fields and values. Here, the order of the fields can differ.
- You can use this operator with methods like update(), findAndModify(), etc., according to your requirements.
Conclusion
Overall, $pull operator in MongoDB is a useful tool for removing specific elements from arrays within documents. By understanding its syntax and usage, developers can efficiently manipulate array data in MongoDB. We have also saw various examples which help us to understand the $pull operator in MongoDB very effectively.
FAQs on MongoDB $pull
Can the $pull operator remove multiple elements from an array?
Yes, the $pull operator can remove multiple elements that match the specified conditions or values from an array in a single operation.
Does the $pull operator modify the original array?
Yes, the $pull operator modifies the original array by removing all instances of the specified elements. If no elements are removed, it returns “{ nModified: 0 }”.
Can the $pull operator be used with nested arrays?
Yes, the $pull operator can be used with nested arrays. It applies the conditions or values to each item in the array as if it were a document in a collection.
How does the $pull operator handle non-existent values?
If the $pull operator cannot find the specified values or conditions in the array, it makes no changes to the array and returns “{ nModified: 0 }”.
Is the order of elements important when using the $pull operator?
Yes, when specifying values to remove from an array, the order must be the same as the order in the array. For documents, the order of fields can differ, but the values must match.


