You are browsing a version that is no longer maintained. |
Aggregation builder
This feature is introduced in version 1.2 |
The aggregation framework provides an easy way to process records and return computed results. The aggregation builder helps to build complex aggregation pipelines.
Creating an Aggregation Builder
You can easily create a new Aggregation\Builder
object with the
DocumentManager::createAggregationBuilder()
method:
The first argument indicates the document for which you want to create the builder.
Adding pipeline stages
To add a pipeline stage to the builder, call the corresponding method on the builder object:
1 <?php
$builder = $dm->createAggregationBuilder(\Documents\Orders::class);
$builder
->match()
->field('purchaseDate')
->gte($from)
->lt($to)
->field('user')
->references($user)
->group()
->field('id')
->expression('$user')
->field('numPurchases')
->sum(1)
->field('amount')
->sum('$amount');
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Just like the query builder, the aggregation builder takes care of converting
DateTime
objects into MongoDB\Driver\BSON\UTCDateTime
objects.
Nesting expressions
You can create more complex aggregation stages by using the expr()
method in
the aggregation builder.
1 <?php
$builder = $dm->createAggregationBuilder(\Documents\Orders::class);
$builder
->match()
->field('purchaseDate')
->gte($from)
->lt($to)
->field('user')
->references($user)
->group()
->field('id')
->expression(
$builder->expr()
->field('month')
->month('$purchaseDate')
->field('year')
->year('$purchaseDate')
)
->field('numPurchases')
->sum(1)
->field('amount')
->sum('$amount');
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This aggregation would group all purchases by their month and year by projecting
those values into an embedded object for the id
field. For example:
Executing an aggregation pipeline
You can execute a pipeline using the execute()
method. This will run the
aggregation pipeline and return a cursor for you to iterate over the results:
If you instead want to look at the built aggregation pipeline, call the
Builder::getPipeline()
method.
Hydration
By default, aggregation results are returned as PHP arrays. This is because the
result of an aggregation pipeline may look completely different from the source
document. In order to get hydrated aggregation results, you first have to map
a QueryResultDocument
. These are written like regular mapped documents, but
they can't be persisted to the database.
Once you have mapped the document, use the hydrate()
method to tell the
aggregation builder about this document:
1 <?php
$builder = $dm->createAggregationBuilder(\Documents\Orders::class);
$builder
->hydrate(\Documents\UserPurchases::class)
->match()
->field('purchaseDate')
->gte($from)
->lt($to)
->field('user')
->references($user)
->group()
->field('id')
->expression('$user')
->field('numPurchases')
->sum(1)
->field('amount')
->sum('$amount');
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
When you run the queries, all results will be returned as instances of the specified document.
Query result documents can use all features regular documents can use: you can map embedded documents, define references, and even use discriminators to get different result documents according to the aggregation result. |
Disabling Result Caching
Due to MongoDB cursors not being rewindable, ODM uses a caching iterator when returning results from aggregation pipelines. This cache allows you to iterate a result cursor multiple times without re-executing the original aggregation pipeline. However, in long-running processes or when handling a large number of results, this can lead to high memory usage. To disable this result cache, you can tell the query builder to not return a caching iterator:
When setting this option to false
, attempting a second iteration will result
in an exception.
Aggregation pipeline stages
MongoDB provides the following aggregation pipeline stages:
- $addFields
- $bucket
- $bucketAuto
- $collStats
- $count
- $facet
- $geoNear
- $graphLookup
- $group
- $indexStats
- $limit
- $lookup
- $match
- $out
- $project
- $redact
- $replaceRoot
- $sample
- $skip
- $sort
- $sortByCount
- $unwind
The |
$addFields
Adds new fields to documents. $addFields
outputs documents that contain all
existing fields from the input documents and newly added fields.
The $addFields
stage is equivalent to a $project
stage that explicitly
specifies all existing fields in the input documents and adds the new fields.
You can also pass expressions as arrays:
This allows usage of any expression operators introduced by MongoDB, even if Doctrine ODM does not yet wrap it with convenience methods.
You can see all available expression operators at MongoDB documentation here.
$bucket
Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.
Each bucket is represented as a document in the output. The document for each bucket contains an _id field, whose value specifies the inclusive lower bound of the bucket and a count field that contains the number of documents in the bucket. The count field is included by default when the output is not specified.
$bucket
only produces output documents for buckets that contain at least one
input document.
$bucketAuto
Similar to $bucket
, except that boundaries are automatically determined in
an attempt to evenly distribute the documents into the specified number of
buckets.
$collStats
The $collStats
stage returns statistics regarding a collection or view.
$count
Returns a document that contains a count of the number of documents input to the stage.
The example above returns a single document with the numSingleItemOrders
containing the number of orders found.
$facet
Processes multiple aggregation pipelines within a single stage on the same set of input documents. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.
1 <?php
$builder = $dm->createAggregationBuilder(\Documents\Orders::class);
$builder
->facet()
->field('groupedByItemCount')
->pipeline(
$dm->createAggregationBuilder(\Documents\Orders::class)->group()
->field('id')
->expression('$itemCount')
->field('lowestValue')
->min('$value')
->field('highestValue')
->max('$value')
->field('totalValue')
->sum('$value')
->field('averageValue')
->avg('$value')
)
->field('groupedByYear')
->pipeline(
$dm->createAggregationBuilder(\Documents\Orders::class)->group()
->field('id')
->year('purchaseDate')
->field('lowestValue')
->min('$value')
->field('highestValue')
->max('$value')
->field('totalValue')
->sum('$value')
->field('averageValue')
->avg('$value')
)
;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$geoNear
The $geoNear
stage finds and outputs documents in order of nearest to
farthest from a specified point.
The |
$graphLookup
Performs a recursive search on a collection, with options for restricting the
search by recursion depth and query filter. The $graphLookup
stage can be
used to resolve association graphs and flatten them into a single list.
The target document of the reference used in |
Due to a limitation in MongoDB, the |
$group
The $group
stage is used to do calculations based on previously matched
documents:
1 <?php
$builder = $dm->createAggregationBuilder(\Documents\Orders::class);
$builder
->match()
->field('user')
->references($user)
->group()
->field('id')
->expression(
$builder->expr()
->field('month')
->month('purchaseDate')
->field('year')
->year('purchaseDate')
)
->field('numPurchases')
->sum(1)
->field('amount')
->sum('$amount');
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$indexStats
The $indexStats
stage returns statistics regarding the use of each index for
the collection. More information can be found in the official Documentation
$lookup
The |
The $lookup
stage is used to fetch documents from different collections in
pipeline stages. Take the following relationship for example:
In MongoDB 3.2, the resulting array will be empty for a one-to-many relationship, you need to unwind your field at first and use a group stage afterwards.
The resulting array will contain all matched item documents in an array. This has to be considered when looking up one-to-one relationships:
MongoDB will always return an array, even if the lookup only returned a single
document. Thus, when looking up one-to-one references the result must be flattened
using the $unwind
operator.
Looking up a reference nested in an embedded document (like ->lookup('embedDoc.refDocs')
)
is not supported. You'll need to make your lookup as if your Reference was not mapped
See below for more.
Due to a limitation in MongoDB, the |
You can also configure your lookup manually if you don't have it mapped in your document:
$match
The $match
stage lets you filter documents according to certain criteria. It
works just like the query builder:
You can also use fields defined in previous stages:
$out
The $out
stage is used to store the result of the aggregation pipeline in a
collection instead of returning an iterable cursor of results. This must be the
last stage in an aggregation pipeline.
If the collection specified by the $out
operation already exists, then upon
completion of the aggregation, the existing collection is atomically replaced.
Any indexes that existed on the collection are left intact. If the aggregation
fails, the $out
operation does not remove the data from an existing
collection.
The aggregation pipeline will fail to complete if the result would violate
any unique index constraints, including those on the |
$redact
The redact stage can be used to restrict the contents of the documents based on
information stored in the documents themselves. You can read more about the
$redact
stage in the MongoDB documentation.
The following example taken from the official documentation checks the level
field on all document levels and evaluates it to grant or deny access:
1 {
_id: 1,
level: 1,
acct_id: "xyz123",
cc: {
level: 5,
type: "yy",
num: 000000000000,
exp_date: ISODate("2015-11-01T00:00:00.000Z"),
billing_addr: {
level: 5,
addr1: "123 ABC Street",
city: "Some City"
},
shipping_addr: [
{
level: 3,
addr1: "987 XYZ Ave",
city: "Some City"
},
{
level: 3,
addr1: "PO Box 0123",
city: "Some City"
}
]
},
status: "A"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$replaceRoot
Promotes a specified document to the top level and replaces all other fields.
The operation replaces all existing fields in the input document, including the
_id
field. You can promote an existing embedded document to the top level,
or create a new document for promotion.
$sample
The sample stage can be used to randomly select a subset of documents in the
aggregation pipeline. It behaves like the $limit
stage, but instead of
returning the first n
documents it returns n
random documents.
$sort, $limit and $skip
The $sort
, $limit
and $skip
stages behave like the corresponding
query options, allowing you to control the order and subset of results returned
by the aggregation pipeline.
$sortByCount
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
Each output document contains two fields: an _id field containing the distinct grouping value, and a count field containing the number of documents belonging to that grouping or category.
The documents are sorted by count in descending order.
The example above is equivalent to the following pipeline:
$unwind
The $unwind
stage flattens an array in a document, returning a copy for each
item. Take this sample document:
To flatten the purchaseDates
array, we would apply the following pipeline
stage:
The stage would return three documents, each containing a single purchase date: