0
0
MongoDBquery~20 mins

$addToSet for unique array additions in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $addToSet Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the resulting array after using $addToSet?

Consider a MongoDB document with the field tags as ["red", "blue"]. You run this update:

{ $addToSet: { tags: "green" } }

What will be the new value of tags?

A["red", "blue", "green", "green"]
B["red", "blue", "green"]
C["green"]
D["red", "blue"]
Attempts:
2 left
💡 Hint

Remember, $addToSet adds the value only if it is not already present.

🧠 Conceptual
intermediate
2:00remaining
What does $addToSet do when the value already exists?

If you use $addToSet to add a value that is already in the array, what happens?

AThe array remains unchanged; no duplicates are added.
BThe entire array is replaced with the new value.
CThe value is added again, creating duplicates.
DAn error is thrown because duplicates are not allowed.
Attempts:
2 left
💡 Hint

Think about the meaning of 'set' in $addToSet.

📝 Syntax
advanced
2:00remaining
Which update query correctly adds multiple unique values using $addToSet?

You want to add the values "yellow" and "purple" to the colors array, ensuring no duplicates. Which query is correct?

A{ $addToSet: { colors: ["yellow", "purple"] } }
B{ $push: { colors: { $each: ["yellow", "purple"] } } }
C{ $addToSet: { colors: { $each: ["yellow", "purple"] } } }
D{ $addToSet: { colors: { $all: ["yellow", "purple"] } } }
Attempts:
2 left
💡 Hint

To add multiple values uniquely, you need a special operator inside $addToSet.

🔧 Debug
advanced
2:00remaining
Why does this $addToSet update add the new value?

Given the document { _id: 1, items: ["apple", "banana"] }, you run this update:

{ $addToSet: { items: { name: "apple" } } }

Why does the new value get added to the array?

ABecause the array already contains the exact object { name: "apple" }.
BBecause $addToSet only works with strings, not objects.
CBecause the update syntax is invalid and does not run.
DBecause the value { name: "apple" } is an object and differs from the string "apple" in the array.
Attempts:
2 left
💡 Hint

Think about how MongoDB compares objects and strings in arrays.

optimization
expert
3:00remaining
How to efficiently add unique nested objects to an array using $addToSet?

You have a collection where each document has an orders array of objects like { productId: 1, quantity: 2 }. You want to add a new order { productId: 3, quantity: 1 } only if it does not already exist (exact match). Which update query is the most efficient and correct?

A{ $addToSet: { orders: { productId: 3, quantity: 1 } } }
B{ $push: { orders: { productId: 3, quantity: 1 } } }
C{ $addToSet: { orders: { $each: [ { productId: 3, quantity: 1 } ] } } }
D{ $addToSet: { orders: { $elemMatch: { productId: 3, quantity: 1 } } } }
Attempts:
2 left
💡 Hint

Consider how $addToSet treats objects and the use of $each or $elemMatch.