MongoDB\Database::aggregate()
New in version 1.5.
Definition
MongoDB\Database::aggregate()
Runs a specified admin/diagnostic pipeline which does not require an underlying collection. For aggregations on collection data, see
MongoDB\Collection::aggregate()
.function aggregate( array $pipeline, array $options = [] ): Traversable
Parameters
$pipeline
: array- Specifies an aggregation pipeline operation.
$options
: arrayAn array specifying the desired options.
NameTypeDescriptionallowDiskUsebooleanEnables writing to temporary files. When set totrue
, aggregation stages can write data to the_tmp
sub-directory in thedbPath
directory.batchSizeintegerSpecifies 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.bypassDocumentValidationbooleancodecMongoDB\Codec\DocumentCodecThe codec to use for encoding or decoding documents. This option is mutually exclusive with the
typeMap
option.New in version 1.17.
collationarray|objectCollation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the
locale
field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.commentmixedEnables 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.
explainbooleanSpecifies whether or not to return the information on the processing of the pipeline.hintstring|array|objectThe 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.
letarray|objectMap 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.
maxTimeMSintegerThe cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
readConcernRead concern to use for the operation. Defaults to the database's read concern.
readPreferenceRead preference to use for the operation. Defaults to the database's read preference.
sessionClient session to associate with the operation.
typeMaparrayThe type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the database's type map.
writeConcernWrite concern to use for the operation. Defaults to the database's write concern.
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 extension level (e.g. connection errors).
Examples
The following aggregation example lists all running commands using the
$currentOp
aggregation pipeline stage, then filters this list to only show
running command operations.
$database = (new MongoDB\Client)->admin; $cursor = $database->aggregate( [ ['$currentOp' => []], ['$match' => ['op' => 'command']], ] );
See Also
aggregate command reference in the MongoDB manual
Aggregation Pipeline documentation in the MongoDB Manual