- Reference >
- MongoDB\Collection Class >
- MongoDB\Collection::aggregate()
MongoDB\Collection::aggregate()
On this page
Definition
-
MongoDB\Collection::aggregate
Executes an aggregation framework pipeline operation on the collection.
This method has the following parameters:
Parameter Type Description $pipeline
array Specifies an aggregation pipeline operation. $options
array Optional. An array specifying the desired options. The
$options
parameter supports the following options:Option Type Description allowDiskUse
boolean Optional. Enables writing to temporary files. When set to true
, aggregation stages can write data to the_tmp
sub-directory in thedbPath
directory.batchSize
integer Optional. Specifies the batch size for the cursor, which will apply to both the initial
aggregate
command and any subsequentgetMore
commands. This determines the maximum number of documents to return in each response from the server.A batchSize of
0
is special in that and will only apply to the initialaggregate
command; subsequentgetMore
commands will use the server’s default batch size. This may be useful for quickly returning a cursor or failure fromaggregate
without doing significant server-side work.bypassDocumentValidation
boolean Optional. If
true
, allows the write operation to circumvent document level validation. Defaults tofalse
.comment
mixed Optional. Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.
The comment can be any valid BSON type for server versions 4.4 and above. Earlier server versions only support string values.
New in version 1.3.
explain
boolean Optional. Specifies whether or not to return the information on the processing of the pipeline.
New in version 1.4.
hint
string|array|object Optional. The index to use. Specify either the index name as a string or the index key pattern as a document. If specified, then the query system will only consider plans using the hinted index.
New in version 1.3.
let
array|object Optional. Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g.
$$var
).This is not supported for server versions prior to 5.0 and will result in an exception at execution time if used.
New in version 1.9.
maxTimeMS
integer Optional. The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point. readConcern
MongoDB\Driver\ReadConcern Optional. Read concern to use for the operation. Defaults to the collection’s read concern.
It is not possible to specify a read concern for individual operations as part of a transaction. Instead, set the
readConcern
option when starting the transaction with startTransaction.readPreference
MongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the collection’s read preference. session
MongoDB\Driver\Session Optional. Client session to associate with the operation.
New in version 1.3.
typeMap
array Optional. The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the collection’s type map. useCursor
boolean Optional. Indicates whether the command will request that the server provide results using a cursor. The default is
true
.Note
MongoDB 3.6+ no longer supports returning results without a cursor (excluding when the explain option is used) and specifying false for this option will result in a server error.
writeConcern
MongoDB\Driver\WriteConcern Optional. Write concern to use for the operation. Defaults to the collection’s write concern.
It is not possible to specify a write concern for individual operations as part of a transaction. Instead, set the
writeConcern
option when starting the transaction with startTransaction.
Return Values
A MongoDB\Driver\Cursor or ArrayIterator object. In both cases, the return value will be Traversable.
Errors/Exceptions
MongoDB\Exception\UnexpectedValueException
if the command
response from the server was malformed.
MongoDB\Exception\UnsupportedException
if options are used and
not supported by the selected server (e.g. collation
, readConcern
,
writeConcern
).
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).
Behavior
MongoDB\Collection::aggregate()
’s return value depends on the
MongoDB server version and whether the useCursor
option is specified. If
useCursor
is true
, a MongoDB\Driver\Cursor object is returned. If useCursor
is
false
, an ArrayIterator is returned that wraps the
result
array from the command response document. In both cases, the return
value will be Traversable.
Examples
The following aggregation example uses a collection called names
and groups
the first_name
field together, counts the total number of results in each
group, and sorts the results by name.
See Also
MongoDB\Database::aggregate()
- aggregate command reference in the MongoDB manual
- Aggregation Pipeline documentation in the MongoDB Manual