Bird
Raised Fist0
MongoDBquery~20 mins

$pull operator for removing from arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
MongoDB $pull Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result after using $pull to remove a specific value?
Given a document { _id: 1, tags: ["red", "blue", "green", "blue"] }, what will be the tags array after applying {$pull: {tags: "blue"}}?
MongoDB
db.collection.updateOne({_id: 1}, {$pull: {tags: "blue"}})
db.collection.findOne({_id: 1})
A{"_id": 1, "tags": ["red", "green"]}
B{"_id": 1, "tags": ["red", "blue", "green"]}
C{"_id": 1, "tags": ["blue", "green"]}
D{"_id": 1, "tags": ["red", "green", "blue"]}
Attempts:
2 left
💡 Hint
The $pull operator removes all instances of the specified value from the array.
query_result
intermediate
2:00remaining
Removing objects from an array using $pull with a condition
Consider a document { _id: 2, items: [{id: 1, qty: 5}, {id: 2, qty: 10}, {id: 3, qty: 3}] }. What will be the items array after applying {$pull: {items: {qty: {$lt: 5}}}}?
MongoDB
db.collection.updateOne({_id: 2}, {$pull: {items: {qty: {$lt: 5}}}})
db.collection.findOne({_id: 2})
A{"_id": 2, "items": []}
B{"_id": 2, "items": [{"id": 2, "qty": 10}]}
C{"_id": 2, "items": [{"id": 1, "qty": 5}, {"id": 2, "qty": 10}]}
D{"_id": 2, "items": [{"id": 1, "qty": 5}, {"id": 3, "qty": 3}]}
Attempts:
2 left
💡 Hint
The $pull condition removes all objects where qty is less than 5.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this $pull update command
Which option contains a syntax error when trying to remove all elements equal to 5 from the array numbers?
MongoDB
db.collection.updateOne({_id: 3}, {$pull: {numbers: 5}})
Adb.collection.updateOne({_id: 3}, {$pull: {numbers: 5}})
Bdb.collection.updateOne({_id: 3}, {$pull: {numbers: {ne: 5}}})
Cdb.collection.updateOne({_id: 3}, {$pull: {numbers: {$eq: 5}}})
Ddb.collection.updateOne({_id: 3}, {$pull: {numbers: {$in: [5]}}})
Attempts:
2 left
💡 Hint
Check the meaning and syntax of the $pull condition operators.
query_result
advanced
2:00remaining
What is the resulting array after $pull with multiple conditions?
Given a document { _id: 4, scores: [10, 20, 30, 40, 50] }, what will be the scores array after applying {$pull: {scores: {$gte: 20, $lte: 40}}}?
MongoDB
db.collection.updateOne({_id: 4}, {$pull: {scores: {$gte: 20, $lte: 40}}})
db.collection.findOne({_id: 4})
A{"_id": 4, "scores": [10, 50]}
B{"_id": 4, "scores": [10, 20, 30, 40, 50]}
C{"_id": 4, "scores": [20, 30, 40]}
D{"_id": 4, "scores": [10, 20, 50]}
Attempts:
2 left
💡 Hint
The $pull condition removes elements between 20 and 40 inclusive.
🧠 Conceptual
expert
3:00remaining
Why does $pull not remove elements when using an incorrect query condition?
You run {$pull: {items: {status: "active"}}} on a document where items is an array of strings like ["active", "inactive", "active"]. Why does $pull not remove any elements?
ABecause $pull cannot remove duplicate elements.
BBecause $pull only works on numeric arrays, not strings.
CBecause $pull requires the use of $eq operator explicitly to match strings.
DBecause $pull matches whole elements, and the condition expects objects but the array contains strings.
Attempts:
2 left
💡 Hint
Think about the data type of array elements and the query condition shape.

Practice

(1/5)
1. What does the $pull operator do in MongoDB?
easy
A. Removes all array elements that match a specified condition.
B. Adds new elements to an array.
C. Replaces the entire array with a new one.
D. Sorts the elements inside an array.

Solution

  1. Step 1: Understand the purpose of $pull

    The $pull operator is used to remove elements from an array that match a given condition.
  2. Step 2: Compare with other array operators

    Unlike $push which adds elements, $pull removes matching elements without affecting others.
  3. Final Answer:

    Removes all array elements that match a specified condition. -> Option A
  4. Quick Check:

    $pull = Remove matching elements [OK]
Hint: Remember: $pull always removes matching array items [OK]
Common Mistakes:
  • Confusing $pull with $push (which adds elements)
  • Thinking $pull replaces the whole array
  • Assuming $pull sorts or modifies elements
2. Which of the following is the correct syntax to remove the value 5 from the array field numbers in MongoDB?
easy
A. db.collection.updateOne({}, {$pull: {numbers: 5}})
B. db.collection.updateOne({}, {$pull: {numbers: {$ne: 5}}})
C. db.collection.updateOne({}, {$remove: {numbers: 5}})
D. db.collection.updateOne({}, {$pop: {numbers: 5}})

Solution

  1. Step 1: Identify the correct operator and syntax

    The $pull operator removes elements matching the value directly, so {$pull: {numbers: 5}} is correct.
  2. Step 2: Check other options for errors

    db.collection.updateOne({}, {$pop: {numbers: 5}}) uses $pop which only removes first/last elements. db.collection.updateOne({}, {$pull: {numbers: {$ne: 5}}}) uses {$ne: 5} inside $pull which removes non-matching elements. db.collection.updateOne({}, {$remove: {numbers: 5}}) uses invalid $remove operator.
  3. Final Answer:

    db.collection.updateOne({}, {$pull: {numbers: 5}}) -> Option A
  4. Quick Check:

    Simple value removal uses $pull: {field: value} [OK]
Hint: Use $pull: {field: value} to remove simple values [OK]
Common Mistakes:
  • Using $remove instead of $pull
  • Using $pop which removes first or last element only
  • Adding unnecessary query operators for simple values
3. Given the document { _id: 1, tags: ["red", "blue", "green", "blue"] }, what will be the tags array after running db.collection.updateOne({_id: 1}, {$pull: {tags: "blue"}})?
medium
A. ["blue", "blue"]
B. ["red", "blue", "green"]
C. ["red", "green", "blue"]
D. ["red", "green"]

Solution

  1. Step 1: Understand $pull removes all matching elements

    The $pull operator removes every element equal to "blue" from the array.
  2. 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.
  3. Final Answer:

    ["red", "green"] -> Option D
  4. Quick Check:

    All "blue" removed = ["red", "green"] [OK]
Hint: Remember $pull removes all matching elements, not just one [OK]
Common Mistakes:
  • Removing only the first matching element
  • Leaving one "blue" element by mistake
  • Confusing $pull with $pop or $pullAll
4. You want to remove all numbers less than 10 from the array field scores. Which of the following update commands will NOT work correctly?
medium
A. db.collection.updateOne({}, {$pull: {scores: { $lt: 10 }}})
B. db.collection.updateOne({}, {$pull: {scores: {$lt: 10}}})
C. db.collection.updateOne({}, {$pull: {scores: { $gte: 10 }}})
D. db.collection.updateOne({}, {$pull: {scores: {$lt:10}}})

Solution

  1. Step 1: Understand the condition to remove numbers less than 10

    The correct condition is {$lt: 10} to remove numbers less than 10.
  2. 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.
  3. Final Answer:

    db.collection.updateOne({}, {$pull: {scores: { $gte: 10 }}}) -> Option C
  4. Quick Check:

    Use $lt to remove less than 10, not $gte [OK]
Hint: Use correct comparison operator inside $pull condition [OK]
Common Mistakes:
  • Using wrong comparison operator ($gte instead of $lt)
  • Confusing $pull condition syntax
  • Repeating same option without change (typo)
5. Consider documents with a field items containing objects like {name: "apple", qty: 5}. How would you remove all items where qty is 0 using $pull?
hard
A. db.collection.updateMany({}, {$pull: {items: {qty: {$ne: 0}}}})
B. db.collection.updateMany({}, {$pull: {items: {qty: 0}}})
C. db.collection.updateMany({}, {$pull: {items: {name: "apple"}}})
D. db.collection.updateMany({}, {$pull: {items: {qty: {$gt: 0}}}})

Solution

  1. Step 1: Identify the condition to remove items with qty 0

    We want to remove array elements where the field qty equals 0.
  2. 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.
  3. Final Answer:

    db.collection.updateMany({}, {$pull: {items: {qty: 0}}}) -> Option B
  4. Quick Check:

    Match exact condition inside $pull to remove objects [OK]
Hint: Use $pull with object condition to remove matching objects [OK]
Common Mistakes:
  • Removing by wrong field (like name instead of qty)
  • Using $ne or $gt incorrectly inside $pull
  • Confusing $pull with $push or $pop