- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $push (aggregation)
$push (aggregation)¶
On this page
Definition¶
-
$push
¶ Returns an array of all values that result from applying an expression to each document in a group of documents that share the same group by key.
$push
is only available in the$group
stage.$push
has the following syntax:For more information on expressions, see Expressions.
Behavior¶
When using $push
in a $group
stage, the order of
the documents in the output array is determined by the order of the
documents coming into pipeline. To guarantee a defined order, the
$group
pipeline stage should follow a $sort
stage.
Memory Restrictions¶
Starting in version 3.6.17, $push
has
a memory limit of 100 MiB (100 * 1024 * 1024 bytes), even when
db.collection.aggregate()
is run with allowDiskUse:
true.
See aggregration pipeline limitations for more information.
Example¶
Consider a sales
collection with the following documents:
Grouping the documents by the day and the year of the date
field,
the following operation uses the $push
accumulator to
compute the list of items and quantities sold for each group:
The operation returns the following results: