C100DBA · Question #1
What does the following aggregate query perform?
The correct answer is C. Calculates the number of posts with likes between 100 and 200. Option C is correct because the pipeline uses $match to filter posts where likes falls between 100 and 200, then uses $group with _id: null and a $sum: 1 accumulator - a classic MongoDB pattern for counting all documents that meet a condition into a single result. Why the…
Question
What does the following aggregate query perform?
Options
- AFetches the posts with likes between 100 and 200 and sets their _id as null
- BGroups the posts by number of likes (101, 102, 103.) by adding 1 every time
- CCalculates the number of posts with likes between 100 and 200
- DFetches the posts with likes between 100 and 200, sets the _id of the first document as null and
How the community answered
(29 responses)- A3% (1)
- B7% (2)
- C76% (22)
- D14% (4)
Explanation
Option C is correct because the pipeline uses $match to filter posts where likes falls between 100 and 200, then uses $group with _id: null and a $sum: 1 accumulator - a classic MongoDB pattern for counting all documents that meet a condition into a single result.
Why the distractors are wrong:
- A is wrong because the query doesn't merely fetch posts - it aggregates them; setting
_id: nullin a$groupstage is a grouping mechanism, not a document field update. - B is wrong because
$groupwith_id: nullcollapses all matched documents into one group; it doesn't group by individual like counts (101, 102, 103…) or increment anything iteratively. - D is a partial/misleading description - it conflates
$match(filtering) with$group(aggregating) and misrepresents what_id: nulldoes.
Memory tip: In MongoDB aggregation, _id: null inside $group means "treat the entire result set as one group" - pair it with { $sum: 1 } and you're always counting, not fetching or modifying. Think: null = no grouping key = total count.
Topics
Community Discussion
No community discussion yet for this question.