0
0
MongoDBquery~10 mins

One-to-many embedding pattern 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 embed multiple comments inside a blog post document.

MongoDB
db.posts.insertOne({ title: "My Post", comments: [[1]] })
Drag options to blanks, or click blank then click option'
A{ text: "Great post!" }
B[{ text: "Great post!" }, { text: "Thanks for sharing." }]
C"Great post!", "Thanks for sharing."
D{ text: "Great post!" }, { text: "Thanks for sharing." }
Attempts:
3 left
💡 Hint
Common Mistakes
Putting comments as strings instead of objects.
Wrapping the array of comments in extra brackets.
2fill in blank
medium

Complete the query to find posts that have at least one comment with text 'Nice!'.

MongoDB
db.posts.find({ comments: { $elemMatch: { text: [1] } } })
Drag options to blanks, or click blank then click option'
ANice!
B"Nice!"
C{ $eq: "Nice!" }
D/Nice!/
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Using a regex instead of exact match.
3fill in blank
hard

Fix the error in the update to add a new comment to the post with _id 1.

MongoDB
db.posts.updateOne({ _id: 1 }, { [1]: { comments: { text: "New comment" } } })
Drag options to blanks, or click blank then click option'
A$push
B$add
C$set
D$insert
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set which replaces the whole array instead of adding.
Using non-existent operators like $add or $insert.
4fill in blank
hard

Fill both blanks to update the text of the first comment in the post with _id 2.

MongoDB
db.posts.updateOne({ _id: 2 }, { [1]: { "comments.[2].text": "Updated comment" } })
Drag options to blanks, or click blank then click option'
A$set
B$push
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push which adds a new comment instead of updating.
Using index 1 which targets the second comment.
5fill in blank
hard

Fill the blanks to remove comments with text 'Spam' from the post with _id 3.

MongoDB
db.posts.updateOne({ _id: 3 }, { [1]: { comments: { text: [2] } } })
Drag options to blanks, or click blank then click option'
A$pull
B$pop
C"Spam"
D"spam"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $pop which removes only the first or last element.
Using wrong case in the string value.