Complete the code to embed multiple comments inside a blog post document.
db.posts.insertOne({ title: "My Post", comments: [[1]] })The comments field is an array of embedded documents. Each comment is an object inside the array.
Complete the query to find posts that have at least one comment with text 'Nice!'.
db.posts.find({ comments: { $elemMatch: { text: [1] } } })The $elemMatch operator matches array elements. The text field must equal the string "Nice!".
Fix the error in the update to add a new comment to the post with _id 1.
db.posts.updateOne({ _id: 1 }, { [1]: { comments: { text: "New comment" } } })$set which replaces the whole array instead of adding.$add or $insert.The $push operator adds an element to an array field. Here, it adds a new comment object to the comments array.
Fill both blanks to update the text of the first comment in the post with _id 2.
db.posts.updateOne({ _id: 2 }, { [1]: { "comments.[2].text": "Updated comment" } })$push which adds a new comment instead of updating.Use $set to update a specific field. The first comment is at index 0 in the comments array.
Fill the blanks to remove comments with text 'Spam' from the post with _id 3.
db.posts.updateOne({ _id: 3 }, { [1]: { comments: { text: [2] } } })$pop which removes only the first or last element.The $pull operator removes all array elements that match a condition. Here, it removes comments where text equals "Spam".