- Reference >
- Database Commands >
- Aggregation Commands >
- aggregate
aggregate¶
On this page
-
aggregate
¶ Performs aggregation operation using the aggregation pipeline. The pipeline allows users to process data from a collection with a sequence of stage-based manipulations.
Changed in version 2.6.
- The
aggregate
command adds support for returning a cursor, supports theexplain
option, and enhances its sort operations with an external sort facility. - aggregation pipeline introduces the
$out
operator to allowaggregate
command to store results to a collection.
The command has following syntax:
Changed in version 2.6.
The
aggregate
command takes the following fields as arguments:Field Type Description aggregate
string The name of the collection to as the input for the aggregation pipeline. pipeline
array An array of aggregation pipeline stages that process and transform the document stream as part of the aggregation pipeline. explain
boolean Optional. Specifies to return the information on the processing of the pipeline.
New in version 2.6.
allowDiskUse
boolean Optional. Enables writing to temporary files. When set to
true
, aggregation stages can write data to the_tmp
subdirectory in thedbPath
directory.New in version 2.6.
cursor
document Optional. Specify a document that contains options that control the creation of the cursor object.
New in version 2.6.
- The
For more information about the aggregation pipeline Aggregation Pipeline, Aggregation Reference, and Aggregation Pipeline Limits.
Example¶
Aggregate Data with Multi-Stage Pipeline¶
A collection articles
contains documents such as the following:
The following example performs an aggregate
operation on
the articles
collection to calculate the count of each distinct
element in the tags
array that appears in the collection.
In the mongo
shell, this operation can use the
aggregate()
helper as in the following:
Note
In 2.6 and later, the aggregate()
helper always returns a cursor.
Changed in version 2.4: If an error occurs, the aggregate()
helper
throws an exception. In previous versions, the helper returned a
document with the error message and code, and ok
status field
not equal to 1
, same as the aggregate
command.
Return Information on the Aggregation Operation¶
The following aggregation operation sets the optional field explain
to true
to return information about the aggregation operation.
Note
The intended readers of the explain
output document are humans, and
not machines, and the output format is subject to change between
releases.
See also
db.collection.aggregate()
method
Aggregate Data using External Sort¶
Aggregation pipeline stages have maximum memory use limit. To handle large datasets, set
allowDiskUse
option to true
to enable writing data to
temporary files, as in the following example:
See also
Aggregate Command Returns a Cursor¶
Note
Using the aggregate
command to return a cursor is a
low-level operation, intended for authors of drivers. Most users
should use the db.collection.aggregate()
helper provided
in the mongo
shell or in their driver. In 2.6 and
later, the aggregate()
helper always
returns a cursor.
The following command returns a document that contains results with which to instantiate a cursor object.
To specify an initial batch size, specify the batchSize
in the
cursor
field, as in the following example:
The {batchSize: 0 }
document specifies the size of the initial
batch size only. Specify subsequent batch sizes to OP_GET_MORE operations as with other MongoDB cursors. A
batchSize
of 0
means an empty first batch and is useful if you
want to quickly get back a cursor or failure message, without doing
significant server-side work.
See also