- Reference >
- MongoDB\Database Class >
- MongoDB\Database::aggregate()
MongoDB\Database::aggregate()
New in version 1.5.
On this page
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()
.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 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.bypassDocumentValidation
boolean Optional. If
true
, allows the write operation to circumvent document level validation. Defaults tofalse
.comment
string Optional. Users can specify an arbitrary string to help trace the operation through the database profiler, currentOp, and logs. explain
boolean Optional. Specifies whether or not to return the information on the processing of the pipeline. 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. 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 database’s read concern. readPreference
MongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the database’s read preference. session
MongoDB\Driver\Session Optional. Client session to associate with the operation. typeMap
array Optional. The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the database’s type map. writeConcern
MongoDB\Driver\WriteConcern Optional. Write 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 driver 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.
See Also
MongoDB\Collection::aggregate()
- aggregate command reference in the MongoDB manual
- Aggregation Pipeline documentation in the MongoDB Manual