Aggregation Questions with Answers

24 practice questions on Aggregation (NoSQL & MongoDB) with answers and step-by-step solutions. Try 10 samples below, then practise all 24 free in the AptiNest app.

Sample questions

Question 1Easy

Which stage groups documents and computes totals?

  1. $group
  2. $sum
  3. $count_by
  4. $bucketize

Answer: A. $group

Explanation

$group groups by an _id expression and can use accumulators like $sum and $avg.

Step-by-step solution

  1. Pick the stage that groups.
  2. Use accumulators inside it.

Why the other options are wrong

  • B. $sum is an accumulator used inside $group.
  • C. There is no $count_by stage; $group does the aggregation.
  • D. $bucket splits values into ranges; it does not compute totals per group.

Question 2Easy

Which stage skips the first n documents?

  1. $jump
  2. $offset
  3. $drop
  4. $skip

Answer: D. $skip

Explanation

$skip drops n documents from the stream.

Step-by-step solution

  1. Name it.

Why the other options are wrong

  • A. There is no $jump stage; $skip skips documents.
  • B. There is no $offset stage; $skip skips documents.
  • C. There is no $drop stage; $skip skips documents.

Question 3Easy

Which MongoDB method runs a multi-stage data processing pipeline?

  1. pipeline()
  2. aggregate()
  3. reduceAll()
  4. group()

Answer: B. aggregate()

Explanation

aggregate() takes an array of stages.

Step-by-step solution

  1. The aggregate() method is used for multi-stage data processing in MongoDB.
  2. It accepts an array of pipeline stages as input.
  3. Each stage performs a specific operation on the data stream.
  4. This allows complex data transformations across multiple steps.
  5. The correct answer is aggregate().

Why the other options are wrong

  • A. pipeline is not a MongoDB method; the multi-stage method is aggregate.
  • C. reduceAll is not a MongoDB method for running stages.
  • D. group is a stage inside a pipeline, not the method that runs the pipeline.

Tip: Use aggregate() for complex data pipelines with multiple stages.

Question 4Easy

Which stage filters documents in an aggregation pipeline?

  1. $filter
  2. $where
  3. $select
  4. $match

Answer: D. $match

Explanation

$match keeps only documents satisfying the conditions, like find's filter.

Step-by-step solution

  1. Filtering stage name.
  2. $match.

Why the other options are wrong

  • A. $filter is an array expression operator.
  • B. $where is a JavaScript query operator.
  • C. There is no $select stage; $match is the filtering stage.

Tip: Put $match early to reduce work.

Question 5Medium

What does this pipeline compute?

db.orders.aggregate([
 {$group: {_id: '$city', total: {$sum: '$amount'}}}
])
  1. The number of orders per city
  2. Total order amount per city
  3. The average amount per city
  4. The largest order overall

Answer: B. Total order amount per city

Explanation

$sum on a field adds that field across each group.

Step-by-step solution

  1. Group by city.
  2. Sum the amount field.

Why the other options are wrong

  • A. Counting would be {$sum: 1}.
  • C. Average would use $avg.
  • D. No maximum is taken; the pipeline sums amounts per city.

Tip: $sum: 1 counts documents.

Question 6Medium

Which stage counts the documents reaching it into a named field?

  1. {$total: 'n'}
  2. {$sumAll: 'n'}
  3. {$count: 'n'}
  4. {$size: 'n'}

Answer: C. {$count: 'n'}

Explanation

$count outputs one document with the count in the given field.

Step-by-step solution

  1. Stage with a field name.

Why the other options are wrong

  • A. There is no $total stage; $count is the counting stage.
  • B. There is no $sumAll stage; $count is the counting stage.
  • D. $size is an array operator.

Question 7Medium

What does $sort: {total: -1} followed by $limit: 3 compute?

  1. Bottom 3 by total
  2. Top 3 by total
  3. First 3 in insertion order
  4. Random 3

Answer: B. Top 3 by total

Explanation

Sort descending then keep three.

Step-by-step solution

  1. -1 is descending.
  2. Limit 3.

Why the other options are wrong

  • A. Ascending would give the bottom.
  • C. Sorting then limiting orders by total, not by insertion order.
  • D. Order is deterministic.

Question 8Medium

What does $project do in a pipeline?

  1. Filters out the documents that fail a given condition, as $match does
  2. Sorts the documents by a chosen key
  3. Reshapes documents by including, excluding or computing fields
  4. Writes the documents to another collection

Answer: C. Reshapes documents by including, excluding or computing fields

Explanation

$project selects and transforms fields.

Step-by-step solution

  1. It acts on fields, not rows.

Why the other options are wrong

  • A. $project reshapes fields; it does not remove documents.
  • B. Sorting is done by $sort, not by $project.
  • D. Writing to a collection is $out or $merge, not $project.

Question 9Hard

What does $facet allow?

  1. Faceted full-text search over a single text index only
  2. Joining two collections by a shared key field
  3. Running several sub-pipelines on the same input in one stage
  4. Creating indexes for each sub-pipeline automatically

Answer: C. Running several sub-pipelines on the same input in one stage

Explanation

$facet outputs multiple result arrays from parallel sub-pipelines.

Step-by-step solution

  1. One input, several outputs.

Why the other options are wrong

  • A. $facet runs sub-pipelines on one input; it is not text search.
  • B. $facet does not join collections; $lookup handles joins.
  • D. $facet does not create indexes for its sub-pipelines.

Question 10Hard

What does {$group: {_id: null, avg: {$avg: '$mark'}}} return?

  1. One document per distinct mark value in the input
  2. One document with the average of mark across all input documents
  3. Nothing, because _id cannot be null in a $group stage
  4. The median mark

Answer: B. One document with the average of mark across all input documents

Explanation

A constant (null) _id puts all documents into one group.

Step-by-step solution

  1. _id null means a single group.
  2. $avg computes the mean.

Why the other options are wrong

  • A. Groups by null, not per mark.
  • C. A null _id in $group is valid and groups all documents together.
  • D. $avg returns the mean, not the median.

Practise all 24 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all NoSQL & MongoDB topics · All topics