0
0
MongoDBquery~20 mins

$push operator for adding to arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this $push update?
Given a document { _id: 1, fruits: ["apple", "banana"] }, what will be the fruits array after running this update?

db.collection.updateOne({ _id: 1 }, { $push: { fruits: "orange" } })
A["apple", "banana", "orange"]
B["orange"]
C["apple", "banana"]
D["apple", "banana", ["orange"]]
Attempts:
2 left
💡 Hint
The $push operator adds the new element to the end of the existing array.
📝 Syntax
intermediate
2:00remaining
Which update query correctly uses $push to add multiple elements?
You want to add the elements "grape" and "melon" to the fruits array in one update. Which query is correct?
Adb.collection.updateOne({ _id: 1 }, { $push: { fruits: { $each: ["grape", "melon"] } } })
Bdb.collection.updateOne({ _id: 1 }, { $push: { fruits: { $append: ["grape", "melon"] } } })
Cdb.collection.updateOne({ _id: 1 }, { $push: { fruits: ["grape", "melon"] } })
Ddb.collection.updateOne({ _id: 1 }, { $push: { fruits: { $add: ["grape", "melon"] } } })
Attempts:
2 left
💡 Hint
Use $each inside $push to add multiple elements.
optimization
advanced
2:00remaining
How to efficiently add unique elements to an array?
You want to add "kiwi" to the fruits array only if it does not already exist. Which update query achieves this efficiently?
Adb.collection.updateOne({ _id: 1 }, { $push: { fruits: "kiwi" } })
Bdb.collection.updateOne({ _id: 1 }, { $addToSet: { fruits: "kiwi" } })
Cdb.collection.updateOne({ _id: 1, fruits: { $ne: "kiwi" } }, { $push: { fruits: "kiwi" } })
Ddb.collection.updateOne({ _id: 1 }, { $push: { fruits: { $unique: "kiwi" } } })
Attempts:
2 left
💡 Hint
Use an operator designed to add unique elements to arrays.
🧠 Conceptual
advanced
2:00remaining
What happens if you $push to a non-array field?
Consider a document { _id: 1, fruits: "apple" }. What happens if you run:

db.collection.updateOne({ _id: 1 }, { $push: { fruits: "banana" } })
AThe update ignores the $push and leaves the field unchanged
BThe update converts the field to an array and adds "banana"
CThe update fails with an error because the field is not an array
DThe field is replaced with the string "banana"
Attempts:
2 left
💡 Hint
Think about the data type requirements for $push.
🔧 Debug
expert
2:00remaining
Why does this $push update not add the element?
You run this update:

db.collection.updateOne({ _id: 1 }, { $push: { fruits: { $each: ["pear"], $slice: 2 } } })

The fruits array was ["apple", "banana"] before. After the update, it remains unchanged. Why?
ABecause $each requires $sort to work properly with $slice
BBecause $slice must be negative to keep the last elements, so the update removed all elements
CBecause $slice is not a valid modifier inside $push
DBecause $slice with a positive number limits the array to that many elements from the start, but the update did not add new elements first
Attempts:
2 left
💡 Hint
Understand how $slice works with $push and $each.