The $pull operator helps you remove specific items from a list inside your data. It makes cleaning up or updating lists easy without changing the whole list.
$pull operator for removing from arrays in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
db.collection.updateOne(
{ <filter> },
{ $pull: { <arrayField>: <condition> } }
)$pull removes all array elements that match the <condition>.
The <condition> can be a value or a query expression.
db.users.updateOne(
{ name: "Alice" },
{ $pull: { hobbies: "reading" } }
)db.users.updateOne(
{ name: "Bob" },
{ $pull: { scores: { $lt: 50 } } }
)db.users.updateOne(
{ name: "Carol" },
{ $pull: { tags: { $in: ["old", "expired"] } } }
)This example creates a product with a list of features. Then it removes the feature "bluetooth" from the features array using $pull. It prints the product before and after the update to show the change.
use testdb; // Insert a sample document db.products.insertOne({ name: "Smartphone", features: ["touchscreen", "camera", "bluetooth", "gps"] }); print("Before $pull operation:"); printjson(db.products.findOne({ name: "Smartphone" })); // Remove "bluetooth" feature from the features array const updateResult = db.products.updateOne( { name: "Smartphone" }, { $pull: { features: "bluetooth" } } ); print("Update result:", tojson(updateResult)); print("After $pull operation:"); printjson(db.products.findOne({ name: "Smartphone" }));
Time complexity: Depends on the size of the array; MongoDB scans the array to find matching elements.
Space complexity: Minimal extra space; modifies the document in place.
Common mistake: Using $pull with a condition that matches no elements will not change the array.
Use $pull when you want to remove specific elements from an array without replacing the whole array. Use $set if you want to replace the entire array.
$pull removes matching elements from arrays inside documents.
It works with simple values or query conditions.
It updates only the matching elements, keeping the rest of the array intact.
Practice
$pull operator do in MongoDB?Solution
Step 1: Understand the purpose of
The$pull$pulloperator is used to remove elements from an array that match a given condition.Step 2: Compare with other array operators
Unlike$pushwhich adds elements,$pullremoves matching elements without affecting others.Final Answer:
Removes all array elements that match a specified condition. -> Option AQuick Check:
$pull= Remove matching elements [OK]
- Confusing $pull with $push (which adds elements)
- Thinking $pull replaces the whole array
- Assuming $pull sorts or modifies elements
numbers in MongoDB?Solution
Step 1: Identify the correct operator and syntax
The$pulloperator removes elements matching the value directly, so{$pull: {numbers: 5}}is correct.Step 2: Check other options for errors
db.collection.updateOne({}, {$pop: {numbers: 5}}) uses$popwhich only removes first/last elements. db.collection.updateOne({}, {$pull: {numbers: {$ne: 5}}}) uses{$ne: 5}inside$pullwhich removes non-matching elements. db.collection.updateOne({}, {$remove: {numbers: 5}}) uses invalid$removeoperator.Final Answer:
db.collection.updateOne({}, {$pull: {numbers: 5}}) -> Option AQuick Check:
Simple value removal uses $pull: {field: value} [OK]
- Using $remove instead of $pull
- Using $pop which removes first or last element only
- Adding unnecessary query operators for simple values
{ _id: 1, tags: ["red", "blue", "green", "blue"] }, what will be the tags array after running db.collection.updateOne({_id: 1}, {$pull: {tags: "blue"}})?Solution
Step 1: Understand $pull removes all matching elements
The$pulloperator removes every element equal to "blue" from the array.Step 2: Remove all "blue" elements from the array
Original array: ["red", "blue", "green", "blue"]. After removal: ["red", "green"] because both "blue" elements are removed.Final Answer:
["red", "green"] -> Option DQuick Check:
All "blue" removed = ["red", "green"] [OK]
- Removing only the first matching element
- Leaving one "blue" element by mistake
- Confusing $pull with $pop or $pullAll
scores. Which of the following update commands will NOT work correctly?Solution
Step 1: Understand the condition to remove numbers less than 10
The correct condition is{$lt: 10}to remove numbers less than 10.Step 2: Analyze each option
Options B, C, and D use{$lt: 10}correctly. db.collection.updateOne({}, {$pull: {scores: { $gte: 10 }}}) uses{$gte: 10}, which removes numbers greater than or equal to 10, the opposite of the goal.Final Answer:
db.collection.updateOne({}, {$pull: {scores: { $gte: 10 }}}) -> Option CQuick Check:
Use $lt to remove less than 10, not $gte [OK]
- Using wrong comparison operator ($gte instead of $lt)
- Confusing $pull condition syntax
- Repeating same option without change (typo)
items containing objects like {name: "apple", qty: 5}. How would you remove all items where qty is 0 using $pull?Solution
Step 1: Identify the condition to remove items with qty 0
We want to remove array elements where the fieldqtyequals 0.Step 2: Use $pull with a query object matching qty: 0
The correct syntax is{$pull: {items: {qty: 0}}}which removes all objects with qty 0.Final Answer:
db.collection.updateMany({}, {$pull: {items: {qty: 0}}}) -> Option BQuick Check:
Match exact condition inside $pull to remove objects [OK]
- Removing by wrong field (like name instead of qty)
- Using $ne or $gt incorrectly inside $pull
- Confusing $pull with $push or $pop
