Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\Collection::aggregate()

On this page

  • Definition
  • Parameters
  • Return Values
  • Errors/Exceptions
  • Behavior
  • Examples
  • See Also
MongoDB\Collection::aggregate()

Executes an aggregation framework pipeline operation on the collection.

function aggregate(
array $pipeline,
array $options = []
): Traversable
$pipeline : array
Specifies an aggregation pipeline operation.
$options : array

An array specifying the desired options.

Name
Type
Description
allowDiskUse
boolean
Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp sub-directory in the dbPath directory.
batchSize
integer

Specifies the batch size for the cursor, which will apply to both the initial aggregate command and any subsequent getMore 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 initial aggregate command; subsequent getMore commands will use the server's default batch size. This may be useful for quickly returning a cursor or failure from aggregate without doing significant server-side work.

bypassDocumentValidation
boolean

If true, allows the write operation to circumvent document level validation. Defaults to false.

This only applies when using the $out and $out stages.

codec
MongoDB\Codec\DocumentCodec

The codec to use for encoding or decoding documents. This option is mutually exclusive with the typeMap option.

Defaults to the collection's codec. Inheritance for a default codec option takes precedence over that of the typeMap option.

New in version 1.17.

collation
array|object

Collation 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.

If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

comment
mixed

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

Specifies whether or not to return the information on the processing of the pipeline.

New in version 1.4.

hint
string|array|object

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

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

The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.

readConcern

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.

readPreference

Read preference to use for the operation. Defaults to the collection's read preference.

session

Client session to associate with the operation.

New in version 1.3.

typeMap
array

The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the collection's type map.

writeConcern

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.

This only applies when a $out or $merge stage is specified.

A MongoDB\Driver\Cursor or ArrayIterator object. In both cases, the return value will be Traversable.

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).

MongoDB\Collection::aggregate()'s returns a MongoDB\Driver\Cursor object.

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.

<?php
$collection = (new MongoDB\Client)->test->names;
$cursor = $collection->aggregate(
[
['$group' => ['_id' => '$first_name', 'name_count' => ['$sum' => 1]]],
['$sort' => ['_id' => 1]],
]
);

Back

__construct()