0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
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.