πŸƒDBQuestpractice MongoDB queries
0 / 110

MongoDB $group and accumulators

$group with $sum, $avg, $min, $max, $push, $addToSet.  Β·  11 practice questions  Β·  Cheat sheet ↓

$group collapses many documents into one per key. _id is the grouping key, which can be a field ("$borough"), a compound object, or null for a single total. Every other output field is an accumulator: $sum (with $sum: 1 for a count), $avg, $min, $max, $push to collect values into an array, $addToSet for unique values, $first and $last for the edge documents of a sorted group.

The classic MongoDB $group by count example is { $group: { _id: "$borough", count: { $sum: 1 } } }, usually followed by { $sort: { count: -1 } }. The surprise for SQL users is that $group keeps nothing it was not told to keep: fields that are not the key or an accumulator vanish. If you need them, accumulate them with $first or $push, or group later in the pipeline.

MongoDB $group practice questions

  1. Count the restaurants in each borough. Output the borough as _id and the count as count. easy restaurants
  2. Count restaurants per cuisine as count, sorted from most to least. Break ties by cuisine name ascending. easy restaurants
  3. For each borough compute the average number of seats as avgSeats. medium restaurants
  4. For each borough return the largest and smallest seats values as maxSeats and minSeats. medium restaurants
  5. For each cuisine collect the restaurant names into an array field called names. medium restaurants
  6. For each borough collect the DISTINCT cuisines served into an array field cuisines. medium restaurants
  7. Compute total revenue per customer: group orders by customerId and sum total into revenue. medium orders
  8. Count restaurants for each combination of cuisine and borough. The _id should be an object with both keys. hard restaurants
  9. Count orders by status as n, but only for orders placed through the web channel. hard orders
  10. Compute the overall average seats across the whole collection as avgSeats, in one group with _id of null. hard restaurants
  11. For each dept return the total salary bill as payroll and the headcount as headcount, sorted by payroll descending. hard employees

Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.

$group and accumulators cheat sheet

The shape

{ $group: {
    _id: "$borough",                    // what to group by
    count: { $sum: 1 },                 // accumulators
    avgSeats: { $avg: "$seats" }
} }

_id is mandatory. _id: null groups everything into one bucket.

Accumulators

AccumulatorDoes
{ $sum: 1 }counts documents
{ $sum: "$total" }adds a field up
{ $avg: "$seats" }mean
{ $min: } / { $max: }extremes
{ $push: "$name" }array of every value
{ $addToSet: "$cuisine" }array of distinct values
{ $first: } / { $last: }needs a $sort before it to mean anything

Compound grouping key

{ $group: { _id: { cuisine: "$cuisine", borough: "$borough" }, n: { $sum: 1 } } }

Sort after grouping

{ $group: { _id: "$cuisine", n: { $sum: 1 } } },
{ $sort:  { n: -1 } },
{ $limit: 5 }

Sorting before $group does not survive the group – regroup, then sort.

Gotchas

  • After $group, only the fields you declared exist. name is gone unless

you pushed or $first-ed it.

  • $avg skips non-numeric and missing values rather than treating them as 0.

Common questions

How do I count documents per group in MongoDB?

Group on the field and sum ones: { $group: { _id: "$cuisine", count: { $sum: 1 } } }. Add { $sort: { count: -1 } } for most-to-least. { $sortByCount: "$cuisine" } does both in one stage.

Why does $group lose my other fields?

$group builds brand-new documents from the key and the accumulators you list; nothing else survives. Keep a field with $first: "$name" (the first in the group's order), collect all values with $push, or move the $group later in the pipeline.

What's the difference between $push and $addToSet?

Both collect values into an array per group. $push keeps every value, including duplicates, in encounter order. $addToSet keeps each distinct value once, with no guaranteed order. Use $addToSet for "which cuisines appear in this borough".

Continue:← Aggregation Basics$unwind & Array Pipelines β†’

MongoDB topics:Finding & ProjectionComparison OperatorsLogical OperatorsElement & TypePattern MatchingSort, Limit & SkipArray QueriesEmbedded DocumentsAggregation Basics$unwind & Array PipelinesJoins & Advanced StagesFull cheat sheet