nerdexam
MongoDB

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…

MongoDB Fundamentals

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)
  • A
    3% (1)
  • B
    7% (2)
  • C
    76% (22)
  • D
    14% (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: null in a $group stage is a grouping mechanism, not a document field update.
  • B is wrong because $group with _id: null collapses 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: null does.

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

#aggregation pipeline#$match#$group#range query

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice