- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.aggregate()
db.collection.aggregate()¶
On this page
Definition¶
-
db.collection.aggregate(pipeline, options)¶ Calculates aggregate values for the data in a collection.
Parameter Type Description pipelinearray A sequence of data aggregation operations or stages. See the aggregation pipeline operators for details.
Changed in version 2.6: The method can still accept the pipeline stages as separate arguments instead of as elements in an array; however, if you do not specify the
pipelineas an array, you cannot specify theoptionsparameter.optionsdocument Optional. Additional options that
aggregate()passes to theaggregatecommand.New in version 2.6: Available only if you specify the
pipelineas an array.The
optionsdocument can contain the following fields and values:Field Type Description explainboolean Optional. Specifies to return the information on the processing of the pipeline. See Return Information on Aggregation Pipeline Operation for an example.
New in version 2.6.
allowDiskUseboolean Optional. Enables writing to temporary files. When set to
true, aggregation operations can write data to the_tmpsubdirectory in thedbPathdirectory. See Perform Large Sort Operation with External Sort for an example.New in version 2.6.
cursordocument Optional. Specifies the initial batch size for the cursor. The value of the
cursorfield is a document with the fieldbatchSize. See Specify an Initial Batch Size for syntax and example.New in version 2.6.
maxTimeMSnon-negative integer Optional. The maximum time period in milliseconds the
getMore()operation will block waiting for new data to be inserted into the capped collection.Requires that the cursor on which this
getMore()is acting is anawaitDatacursor. See theawaitDataparameter forfind().bypassDocumentValidationboolean Optional. Available only if you specify the
$outaggregation operator.Enables
db.collection.aggregateto bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.New in version 3.2.
readConcerndocument Optional. Specifies the read concern. The default level is
"local".To use a read concern level of
"majority", you must use the WiredTiger storage engine and start themongodinstances with the--enableMajorityReadConcerncommand line option (or thereplication.enableMajorityReadConcernsetting if using a configuration file).Only replica sets using
protocol version 1support"majority"read concern. Replica sets running protocol version 0 do not support"majority"read concern.To ensure that a single thread can read its own writes, use
"majority"read concern and"majority"write concern against the primary of the replica set.To use a read concern level of
"majority", you cannot include the$outstage.New in version 3.2.
Returns: A cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explainoption, the document that provides details on the processing of the aggregation operation.If the pipeline includes the
$outoperator,aggregate()returns an empty cursor. See$outfor more information.Changed in version 2.6: The
db.collection.aggregate()method returns a cursor and can return result sets of any size. Previous versions returned all results in a single document, and the result set was subject to a size limit of 16 megabytes.
Behavior¶
Error Handling¶
If an error occurs, the aggregate() helper
throws an exception.
Cursor Behavior¶
In the mongo shell, if the cursor returned from the
db.collection.aggregate() is not assigned to a variable using
the var keyword, then the mongo shell automatically
iterates the cursor up to 20 times. See
Iterate a Cursor in the mongo Shell for handling cursors in the
mongo shell.
Cursors returned from aggregation only supports cursor methods that operate on evaluated cursors (i.e. cursors whose first batch has been retrieved), such as the following methods:
See also
For more information, see
Aggregation Pipeline, Aggregation Reference,
Aggregation Pipeline Limits, and aggregate.
Examples¶
The following examples use the collection orders that contains the
following documents:
Group by and Calculate a Sum¶
The following aggregation operation selects documents with status equal
to "A", groups the matching documents by the cust_id field and
calculates the total for each cust_id field from the sum of the
amount field, and sorts the results by the total field in
descending order:
The operation returns a cursor with the following documents:
The mongo shell iterates the returned cursor automatically
to print the results. See Iterate a Cursor in the mongo Shell for
handling cursors manually in the mongo shell.
Return Information on Aggregation Pipeline Operation¶
The following aggregation operation sets the option explain to
true to return information about the aggregation operation.
The operation returns a cursor with the document that contains detailed
information regarding the processing of the aggregation pipeline. For
example, the document may show, among other details, which index, if
any, the operation used. [1] If the orders collection
is a sharded collection, the document would also show the division of
labor between the shards and the merge operation, and for targeted
queries, the targeted shards.
Note
The intended readers of the explain output document are humans, and
not machines, and the output format is subject to change between
releases.
The mongo shell iterates the returned cursor automatically
to print the results. See Iterate a Cursor in the mongo Shell for
handling cursors manually in the mongo shell.
Perform Large Sort Operation with 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:
Specify an Initial Batch Size¶
To specify an initial batch size for the cursor, use the following
syntax for the cursor option:
For example, the following aggregation operation specifies the
initial batch size of 0 for the cursor:
A batchSize of 0 means an empty
first batch and is useful for quickly returning a cursor or failure
message without doing significant server-side work. Specify subsequent
batch sizes to OP_GET_MORE operations as with
other MongoDB cursors.
The mongo shell iterates the returned cursor automatically
to print the results. See Iterate a Cursor in the mongo Shell for
handling cursors manually in the mongo shell.
Override readConcern¶
The following operation on a replica set specifies a
Read Concern of "majority" to read the
most recent copy of the data confirmed as having been written to a
majority of the nodes.
Note
To use a read concern level of
"majority", you must use the WiredTiger storage engine and start themongodinstances with the--enableMajorityReadConcerncommand line option (or thereplication.enableMajorityReadConcernsetting if using a configuration file).Only replica sets using
protocol version 1support"majority"read concern. Replica sets running protocol version 0 do not support"majority"read concern.To ensure that a single thread can read its own writes, use
"majority"read concern and"majority"write concern against the primary of the replica set.To use a read concern level of
"majority", you cannot include the$outstage.Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.
| [1] | Index Filters can affect the choice of index used. See Index Filters for details. |