0
0
MongoDBquery~10 mins

$addToSet for unique array additions in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a unique value to the 'tags' array using $addToSet.

MongoDB
db.collection.updateOne({ _id: 1 }, { $[1]: { tags: "mongodb" } })
Drag options to blanks, or click blank then click option'
AaddToSet
Bpush
Cset
DaddUnique
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push instead of $addToSet, which can add duplicates.
Using $set which replaces the whole array.
2fill in blank
medium

Complete the code to add multiple unique values to the 'tags' array using $addToSet with $each.

MongoDB
db.collection.updateOne({ _id: 2 }, { $addToSet: { tags: { $[1]: ["nodejs", "database"] } } })
Drag options to blanks, or click blank then click option'
Aall
Bmultiple
Cmany
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push with $each but forgetting $addToSet for uniqueness.
Using incorrect modifier names like $all or $many.
3fill in blank
hard

Fix the error in the update command to correctly add a unique value to the 'categories' array.

MongoDB
db.collection.updateOne({ _id: 3 }, { $[1]: { categories: "tech" } })
Drag options to blanks, or click blank then click option'
Apush
BaddToSet
Cset
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push which can add duplicates.
Using $set which replaces the whole array.
4fill in blank
hard

Fill both blanks to add multiple unique tags to the 'labels' array.

MongoDB
db.collection.updateOne({ _id: 4 }, { $[1]: { labels: { $[2]: ["urgent", "review"] } } })
Drag options to blanks, or click blank then click option'
AaddToSet
Bpush
Ceach
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push instead of $addToSet.
Forgetting to use $each when adding multiple values.
5fill in blank
hard

Fill all three blanks to add a unique uppercase tag to the 'tags' array using an aggregation pipeline update.

MongoDB
db.collection.updateOne({ _id: 5 }, [ { $[1]: { [2]: { $toUpper: "[3]" } } } ])
Drag options to blanks, or click blank then click option'
AconcatArrays
Burgent
CaddToSet
Dtags
Attempts:
3 left
💡 Hint
Common Mistakes
Using $concatArrays which rebuilds the array but allows duplicates.
Using $set which replaces the array.
Wrong field name or forgetting to uppercase.