- 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. The default isfalse
.batchSize
integer Optional. Specifies the initial batch size 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.Note
This is not supported for inline aggregation results (i.e.
useCursor
option isfalse
or the server version is < 2.6).bypassDocumentValidation
boolean Optional. If
true
, allows the write operation to circumvent document level validation. Defaults tofalse
.This option is available in MongoDB 3.2+ and is ignored for older server versions, which do not support document level validation.
This only applies when using the $out stage.
Document validation requires MongoDB 3.2 or later: if you are using an earlier version of MongoDB, this option will be ignored.
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.
This is not supported for server versions prior to 3.2 and will result in an exception at execution time if used.
readPreference
MongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the collection’s read preference. 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
.For MongoDB version 2.6 or later,
useCursor
allows users to turn off cursors if necessary to aid in replica set or shard cluster upgrades.useCursor
is ignored for MongoDB versions prior to 2.6 as aggregation cursors are not available.writeConcern
MongoDB\Driver\WriteConcern Optional. Write concern to use for the operation. Defaults to the collection’s write concern.
This only applies when the $out stage is specified.
This is not supported for server versions prior to 3.4 and will result in an exception at execution time if used.
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.
See Also
- aggregate command reference in the MongoDB manual
- Aggregation Pipeline documentation in the MongoDB Manual