Consider that the posts collection contains an array called ratings which contains ratings given to the post by various users in the following format:
{
_id: 1,
post_text: "This is my first post",
ratings: [5, 4, 2, 5],
//other elements of document
}
Which of the following query will return all the documents where the rating array contains elements that in some combination satisfy the query conditions?
- db.inventory.find( { ratings: { $elemMatch: { $gt: 3, $lt: 6 } } } )
- db.inventory.find( { ratings: { ratings: { $gt: 5, $lt: 9 } } } )
- db.inventory.find( { ratings: { ratings.$: { $gt: 5, $lt: 9 } } } )
- db.inventory.find( { ratings: { $elemMatch: { $gte: 3, $lte: 6 } } } )
Correct Answer:
B.db.inventory.find( { ratings: { ratings: { $gt: 5, $lt: 9 } } } )
need an explanation for this answer? contact us directly to get an explanation for this answer