MongoDB Query to Pull Elements from Array
Use the
$pull operator in MongoDB to remove elements from an array that match a specified condition, for example: db.collection.updateOne({ _id: 1 }, { $pull: { tags: 'red' } }) removes 'red' from the tags array.Examples
Input{ _id: 1, tags: ['red', 'blue', 'green'] }, query: { $pull: { tags: 'red' } }
Output{ _id: 1, tags: ['blue', 'green'] }
Input{ _id: 2, scores: [10, 20, 30, 20] }, query: { $pull: { scores: 20 } }
Output{ _id: 2, scores: [10, 30] }
Input{ _id: 3, items: ['apple', 'banana', 'cherry'] }, query: { $pull: { items: { $in: ['banana', 'cherry'] } } }
Output{ _id: 3, items: ['apple'] }
How to Think About It
To pull elements from an array in MongoDB, think about specifying which elements you want to remove by using the
$pull operator with a condition. MongoDB will then find all matching elements in the array and remove them.Algorithm
1
Identify the document(s) to update using a filter.2
Specify the array field and the condition for elements to remove using <code>$pull</code>.3
Run the update operation to remove matching elements from the array.4
Verify the array no longer contains the pulled elements.Code
mongodb
db.products.insertOne({ _id: 1, tags: ['red', 'blue', 'green'] });
db.products.updateOne(
{ _id: 1 },
{ $pull: { tags: 'red' } }
);
printjson(db.products.findOne({ _id: 1 }));Output
{
"_id" : 1,
"tags" : [ "blue", "green" ]
}
Dry Run
Let's trace pulling 'red' from the tags array in the document with _id 1.
1
Initial Document
{ _id: 1, tags: ['red', 'blue', 'green'] }
2
Apply $pull Operator
Remove 'red' from tags array
3
Resulting Document
{ _id: 1, tags: ['blue', 'green'] }
| Step | Array Before | Element Pulled | Array After |
|---|---|---|---|
| 1 | ['red', 'blue', 'green'] | 'red' | ['blue', 'green'] |
Why This Works
Step 1: Use $pull to Remove Elements
The $pull operator removes all array elements that match the given condition.
Step 2: Specify Condition
You provide the value or query condition inside $pull to target specific elements.
Step 3: Update Operation
MongoDB updates the document by removing matching elements from the array in place.
Alternative Approaches
Using $pull with $in operator
mongodb
db.collection.updateOne({ _id: 3 }, { $pull: { items: { $in: ['banana', 'cherry'] } } });Removes multiple specified elements from the array in one operation.
Using $set with $filter aggregation (MongoDB 4.2+)
mongodb
db.collection.updateOne(
{ _id: 1 },
[{ $set: { tags: { $filter: { input: '$tags', cond: { $ne: ['$$this', 'red'] } } } } }]
);Filters array elements by condition, useful for complex filtering but less straightforward than $pull.
Complexity: O(n) time, O(1) space
Time Complexity
The operation scans the array elements once to find matches, so it is linear in the size of the array.
Space Complexity
The update happens in place without extra space proportional to the array size.
Which Approach is Fastest?
$pull is efficient for simple removals; using $filter with aggregation can be slower but more flexible.
| Approach | Time | Space | Best For |
|---|---|---|---|
| $pull | O(n) | O(1) | Simple element removal by value or condition |
| $pull with $in | O(n) | O(1) | Removing multiple specific elements |
| $set with $filter | O(n) | O(1) | Complex filtering conditions on arrays |
Use
$pull to remove array elements matching a condition without replacing the whole array.Trying to use
$pull without specifying a condition or using it on a non-array field.