C100DBA · Question #108
Consider that the posts collection contains an array called ratings which contains ratings given to the post by various users in the following format: Which of the following query will return all the
The correct answer is D. db.inventory.find( { ratings: { ratings: { $gt: 5, $lt: 9 } } } ). Option D represents a plain range query on the ratings array without $elemMatch, which is exactly how MongoDB satisfies conditions across elements in combination - one array element can satisfy $gt: 5 while a different element satisfies $lt: 9, and the document still matches. Opt
Question
Consider that the posts collection contains an array called ratings which contains ratings given to the post by various users in the following format:
Which of the following query will return all the documents where the ratings array contains elements that in some combination satisfy the query conditions?
Exhibit
Options
- Adb.inventory.find( { ratings: { $elemMatch: { $gte: 3, $lte: 6 } } } )
- Bdb.inventory.find( { ratings: { $elemMatch: { $gt: 3, $lt: 6 }
- Cdb.inventory.find( { ratings: { ratings.$: { $gt: 5, $lt: 9 } } } )
- Ddb.inventory.find( { ratings: { ratings: { $gt: 5, $lt: 9 } } } )
How the community answered
(40 responses)- A15% (6)
- B10% (4)
- C5% (2)
- D70% (28)
Explanation
Option D represents a plain range query on the ratings array without $elemMatch, which is exactly how MongoDB satisfies conditions across elements in combination - one array element can satisfy $gt: 5 while a different element satisfies $lt: 9, and the document still matches.
Options A and B are the key distractors: both use $elemMatch, which is the opposite behavior - it requires a single element to satisfy all conditions simultaneously. These would only return documents where one rating is both greater than and less than the specified bounds at the same time, which is far more restrictive.
Option C uses ratings.$ which is the positional operator $ - this is only valid in update operations or projections, not in query filter conditions, making it syntactically invalid here.
Memory tip: Think of $elemMatch as "one element must do it all," while a plain array range query is "tag-team allowed - different elements can cover different conditions." The phrase "in some combination" in exam questions is a direct signal that $elemMatch is wrong and a plain query is right.
Note for exam takers: Option D as written (
{ ratings: { ratings: { $gt: 5, $lt: 9 } } }) appears to have a nesting typo - the canonical MongoDB syntax for this pattern is{ ratings: { $gt: 5, $lt: 9 } }. The exam is testing the conceptual distinction, not the exact syntax.
Topics
Community Discussion
No community discussion yet for this question.
