- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $group (aggregation)
$group (aggregation)¶
On this page
Definition¶
-
$group
¶ The
$group
stage separates documents into groups according to a “group key”. The output is one document for each unique group key.A group key is often a field, or group of fields. The group key can also be the result of an expression. Use the
_id
field in the$group
pipeline stage to set the group key. See below for usage examples.In the
$group
stage output, the_id
field is set to the group key for that document.The output documents can also contain additional fields that are set using accumulator expressions.
Note
$group
does not order its output documents.The
$group
stage has the following prototype form:Field Description _id
Required. The _id
expression specifies the group key. If you specify an_id
value of null, or any other constant value, the$group
stage returns a single document that aggregates values across all of the input documents. See the Group by Null example.field
Optional. Computed using the accumulator operators. The
_id
and the accumulator operators can accept any validexpression
. For more information on expressions, see Expressions.
Considerations¶
Accumulator Operator¶
The <accumulator>
operator must be one of the following accumulator
operators:
Changed in version 5.0.
Name | Description |
---|---|
$accumulator |
Returns the result of a user-defined accumulator function. |
$addToSet |
Returns an array of unique expression values for each group. Order of the array elements is undefined. Changed in version 5.0: Available in |
$avg |
Returns an average of numerical values. Ignores non-numeric values. Changed in version 5.0: Available in |
$bottom |
Returns the bottom element within a group according to the specified sort order. New in version 5.2. Available in |
$bottomN |
Returns an aggregation of the bottom New in version 5.2. Available in |
$count |
Returns the number of documents in a group. Distinct from the New in version 5.0: Available in |
$first |
Returns a value from the first document for each group. Order is only defined if the documents are sorted. Distinct from the Changed in version 5.0: Available in |
$firstN |
Returns an aggregation of the first New in version 5.2: Available in |
$last |
Returns a value from the last document for each group. Order is only defined if the documents are sorted. Distinct from the Changed in version 5.0: Available in |
$lastN |
Returns an aggregation of the last New in version 5.2: Available in |
$max |
Returns the highest expression value for each group. Changed in version 5.0: Available in |
$maxN |
Returns an aggregation of the New in version 5.2. Available in |
$mergeObjects |
Returns a document created by combining the input documents for each group. |
$min |
Returns the lowest expression value for each group. Changed in version 5.0: Available in |
$push |
Returns an array of expression values for documents in each group. Changed in version 5.0: Available in |
$stdDevPop |
Returns the population standard deviation of the input values. Changed in version 5.0: Available in |
$stdDevSamp |
Returns the sample standard deviation of the input values. Changed in version 5.0: Available in |
$sum |
Returns a sum of numerical values. Ignores non-numeric values. Changed in version 5.0: Available in |
$top |
Returns the top element within a group according to the specified sort order. New in version 5.2. Available in |
$topN |
Returns an aggregation of the top New in version 5.2. Available in |
$group
and Memory Restrictions¶
The $group
stage has a limit of 100 megabytes of RAM. By
default, if the stage exceeds this limit, $group
returns an
error. To allow more space for stage processing, use the
allowDiskUse option to enable
aggregation pipeline stages to write data to temporary files.
See also
$group
Performance Optimizations¶
This section describes optimizations to improve the performance of
$group
. There are optimizations that you can make manually
and optimizations MongoDB makes internally.
Optimization to Return the First Document of Each Group¶
If a pipeline sorts
and groups
by the same field and the $group
stage only uses the
$first
accumulator operator, consider adding an index on the grouped field which matches the sort order. In some
cases, the $group
stage can use the index to quickly find
the first document of each group.
Example
If a collection named foo
contains an index { x: 1, y: 1 }
,
the following pipeline can use that index to find the first document
of each group:
|sbe-title|¶
Starting in version 5.2, MongoDB uses the slot-based execution
query engine to execute $group
stages
if either:
$group
is the first stage in the pipeline.- All preceding stages in the pipeline can also be executed by the |sbe-short|.
For more information, see $group Optimization.
Examples¶
Count the Number of Documents in a Collection¶
In mongosh
, create a sample collection named
sales
with the following documents:
The following aggregation operation uses the $group
stage
to count the number of documents in the sales
collection:
The operation returns the following result:
This aggregation operation is equivalent to the following SQL statement:
See also
Retrieve Distinct Values¶
The following aggregation operation uses the $group
stage
to retrieve the distinct item values from the sales
collection:
The operation returns the following result:
Group by Item Having¶
The following aggregation operation groups documents by the item
field, calculating the total sale amount per item and returning only
the items with total sale amount greater than or equal to 100:
- First Stage:
- The
$group
stage groups the documents byitem
to retrieve the distinct item values. This stage returns thetotalSaleAmount
for each item. - Second Stage:
- The
$match
stage filters the resulting documents to only return items with atotalSaleAmount
greater than or equal to 100.
The operation returns the following result:
This aggregation operation is equivalent to the following SQL statement:
See also
Calculate Count, Sum, and Average¶
In mongosh
, create a sample collection named
sales
with the following documents:
Group by Day of the Year¶
The following pipeline calculates the total sales amount, average sales quantity, and sale count for each day in the year 2014:
- First Stage:
- The
$match
stage filters the documents to only pass documents from the year 2014 to the next stage. - Second Stage:
- The
$group
stage groups the documents by date and calculates the total sale amount, average quantity, and total count of the documents in each group. - Third Stage:
- The
$sort
stage sorts the results by the total sale amount for each group in descending order.
The operation returns the following results:
This aggregation operation is equivalent to the following SQL statement:
See also
$match
$sort
db.collection.countDocuments()
which wraps the$group
aggregation stage with a$sum
expression.
Group by null
¶
The following aggregation operation specifies a group _id
of
null
, calculating the total sale amount, average quantity, and count of
all documents in the collection.
The operation returns the following result:
This aggregation operation is equivalent to the following SQL statement:
See also
$count
db.collection.countDocuments()
which wraps the$group
aggregation stage with a$sum
expression.
Pivot Data¶
In mongosh
, create a sample collection named
books
with the following documents:
Group title
by author
¶
The following aggregation operation pivots the data in the books
collection to have titles grouped by authors.
The operation returns the following documents:
Group Documents by author
¶
The following aggregation operation groups documents by author
:
- First Stage:
$group
uses the$$ROOT
system variable to group the entire documents by authors. This stage passes the following documents to the next stage:- Second Stage:
$addFields
adds a field to the output containing the total copies of books for each author.Note
The resulting documents must not exceed the
BSON Document Size
limit of 16 megabytes.The operation returns the following documents:
See also
Additional Resources¶
The Aggregation with the Zip Code Data Set
tutorial provides an extensive example of the $group
operator in a common use case.