Search…
Aggregating
Pipeline aggregation allows for querying, aggregating, and transforming data in multiple stages in real-time through a single API request. While query arguments allow for making simple, single-stage queries through the API, pipelines allow for more complex searches, aggregations and transformations.
To leverage pipeline aggregation via the API, simply pass a pipeline query argument with the request. The pipeline query argument is an array of pipeline stages that execute in order. Each stage transforms the result set and passes it on to the next stage in the pipeline.
1
?pipeline=[{ stage1 },{ stage2 }, { stage3 }, ...]
Copied!
In the below example, the pipeline contains two stages. The first is a $match stage. This filters the result set to all accounts create on or after 2015-01-01T00:00:00.000Z. Then that set of results is passed onto the $group stage which counts all of the accounts in the result set and finally returns a count result. For all available stages, see Pipeline Stages
Pipeline Request
Pipeline Result
1
GET /accounts?pipeline=[{"$match": {"created": {"$gte": "2015-01-01T00:00:00.000Z"}}}, {"$group": {"_id": null, "count": {"$count": "_id"}}}]
Copied!
1
{
2
"data": [
3
{
4
"count": 24
5
}
6
],
7
"hasMore": false,
8
"object": "list"
9
}
Copied!

Pipeline Stages

Name
Description
Example
$group
A JSON object containing an _id property (required) and 0 or more property groupings.
{"$group": {"_id": null, "count": {"$count": "_id"}}}
$limit
An integer representing the number of results to return.
{"$limit": 100}
$match
A JSON object containing operators and expressions used to limit the search results.
{"$match": {“$and”: [{“created”: {“$gt”: “2015-01-01T00:00:00.000Z”}}, {“created”: {“$lt”: “2015-06-01T00:00:00.000Z”}}]}}
$project
A JSON object that specifies existing properties or new properties to pass along to the next stage in the pipeline.
{"$project",{"_id": 1, "name": 1}}
$skip
An integer representing the number of results to skip. Useful for paging where mapping or groupings have been applied, or where search the search criteria cannot be paged using unique fields.
{"$skip": 10}
$sort
A JSON object containing one or more properties, the values of which must be 1 for ascending and -1 for descending.
{"$sort": {"_id": -1}}
$unwind
Accepts a property name and deconstructs an array into a single property in multiple documents.
{"$unwind": roles}

$group

Result sets can be grouped in order to apply accumulators and aggregate data from accessible documents.
The $group stage takes the following form:
1
{"$group": {"_id": <expression>, <property>: { <accumulator1> : <expression> }, <property>: { <accumulator2> : <expression> },... }}
Copied!
For available accumulators, see Accumulator Operators.
The _id property is your grouping expression and is required. The _id can be null, it can be a single property or a combination of properties and group expressions. When you specify an _id of null, you will group together all of the documents in the result set. This is helpful for calculating accumulated values for all of the documents, for example when calculating a cont of the total number of documents in your result. When the _id is null, it is not included in the result set.

Examples

In the following example, we aggregate a custom c_patient object with a two-stage pipeline. The first stage is a $match that filters the documents down to patients with a gender: 'f'. Those documents are then passed into the $group stage where we count all of the documents in the result set and return a count property. Because we pass null for the grouping _id, we can calculate an accumulation for the entire result set.
Request
Response
1
GET /c_patients?pipeline=[{"$match": {"gender": "f"}},{"$group": {"_id": null, "count": {"$count": "_id"}}}]
Copied!
1
{
2
"data": [
3
{
4
"count": 359
5
}
6
],
7
"hasMore": false,
8
"object": "list"
9
}
Copied!
The following example is a single-stage $group on the connections object. The grouping is by the connections' context._id. Then, a count is calculated for each context grouping and returned in total properties.
Request
Response
1
GET /connections?pipeline=[{"$group": {"_id": "context._id", "total": {"$count": "_id"}}}, {"$sort": {"total": 1}}]
Copied!
1
{
2
"data": [
3
{
4
"_id": "507f1f77bcf86cd799439011",
5
"total": 2
6
},
7
{
8
"_id": "55ff9db67a00a89c14aa31a6",
9
"total": 4
10
},
11
{
12
"_id": "4d656461626c6552756c656b",
13
"total": 12
14
},
15
{
16
"_id": "507f191e810c19729de860ea",
17
"total": 52
18
}
19
],
20
"hasMore": false,
21
"object": "list"
22
}
Copied!

$limit

The limit stage limits the number of documents passed onto the next stage in the pipeline.
1
{"$limit": <integer>}
Copied!

Example

Request
Response
1
GET /c_patients?pipeline=[{"$match": {"c_gender": "f"}},{"$project": {"c_name": 1}},{"$limit":2}]
Copied!
1
{
2
"data": [
3
{
4
"_id": "59495fc1514ef1010000598d",
5
"c_name": {
6
"c_first": "Jane",
7
"c_last": "Smith"
8
}
9
},
10
{
11
"_id": "59496035bfd25d0100661faf",
12
"c_name": {
13
"c_first": "Holly",
14
"c_last": "Forrester"
15
}
16
}
17
],
18
"hasMore": false,
19
"object": "list"
20
}
Copied!

$match

The $match stage accepts a JSON document that specifies query conditions on indexed properties to filter the resulting document list. This document list is then passed onto the next stage of the pipeline or returned as the response if it is the last stage of the pipeline.
1
{"$match": {<query conditions>}}
Copied!
See Query Operators for more details and examples of available conditions.

Examples

In this example, we have just a one stage pipeline where we match accounts by the first name in a custom script. This returns a result set of accounts filtered to only include accounts where the last name starts with 's'.
Untitled
Response
1
GET /accounts?pipeline=[{"$match": {"name.last": {"$regex": "/^s/i"}}}]
Copied!
1
{
2
"data": [
3
{
4
"_id": "5888b91d35f8c91536c6ddea",
5
"access": 6,
6
"c_enrollments": [],
7
"c_study_groups": [],
8
"created": "2017-01-25T14:41:33.297Z",
9
"email": "[email protected]",
10
"favorite": false,
11
"gender": "m",
12
"inherited_roles": [
13
"000000000000000000000007",
14
"000000000000000000000006"
15
],
16
"key": {
17
"fingerprint": "8e53d430-e30c-11e6-aaeb-99172490c13b",
18
"secret": "heBcf2Wgh6hjezADTAc88wHJNGcJgwUd"
19
},
20
"locale": "en_US",
21
"locked": false,
22
"mobile": "+12223334449",
23
"name": {
24
"first": "John",
25
"last": "Silver"
26
},
27
"object": "account",
28
"roles": [
29
"000000000000000000000004"
30
],
31
"shared": false,
32
"state": "verified",
33
"updated": "2017-01-25T14:42:54.970Z",
34
"updater": {
35
"_id": "5888b91d35f8c91536c6ddea",
36
"object": "account",
37
"path": "/accounts/5888b91d35f8c91536c6ddea"
38
}
39
}
40
],
41
"hasMore": false,
42
"object": "list"
43
}
Copied!

$project

The projection stage takes a JSON document that can specify the inclusion of properties and/or the addition of new properties.
1
{"$project": {<projection spec>}}
Copied!

Specifications

Specification
Description
property: 1 or true
Include this property in the projection
property: expression
Add a new property to the projection

Considerations

    Except for _id, only properties that you specify for inclusion or add to the projection as new properties will be projected on to the next stage.
    The _id property is always included in the projection, even if you don't specify inclusion.
    Existing properties cannot be overridden in a projection. If you wish to transform an existing property in a projection, it is recommended that you instead add a new property with a different name.

Examples

In the below example, a custom c_patient object is aggregated with a two stage pipeline in a script. The first stage is a $match that finds all patient records with c_gender: 'm'. That result set is then passed into the $project stage. The projection then specifies that c_name, and c_gender properties are included and that fullName and age are added as new properties.
    The new fullName property is a concatenation of the c_first and c_last sub-properties if the c_name document property using the $concat string operator and $string literal operator.
    The new age property is calculated from an existing c_birthdate property, using $subtract, $divide, and $floor arithmetic operators to calculate the age from the difference between c_birthdate and today's date.
    Since the $project stage is the last stage in the pipeline, the result of the projection is returned as the response.
Project Example
Project Result
1
return org.objects.c_patients.aggregate()
2
.match({
3
c_gender: 'm'
4
})
5
.project({
6
c_name: 1,
7
fullName: {$concat: ['c_name.c_first', {$string: ' '}, 'c_name.c_last']},
8
c_gender: 1,
9
age: { $floor: { $divide: [{ $subtract:[new Date(), 'c_birthdate']}, (365 * 24*60*60*1000)]}}
10
})
11
.toList();
Copied!
1
{
2
"data": [
3
{
4
"_id": "59495fb2514ef1010000598b",
5
"age": 38,
6
"c_gender": "m",
7
"c_name": {
8
"c_first": "John",
9
"c_last": "Smith"
10
},
11
"fullName": "John Smith"
12
},
13
{
14
"_id": "59495fd7514ef1010000598f",
15
"age": 48,
16
"c_gender": "m",
17
"c_name": {
18
"c_first": "Charles",
19
"c_last": "Jones"
20
},
21
"fullName": "Charles Jones"
22
},
23
{
24
"_id": "59496012cfc40501006c10d2",
25
"age": 43,
26
"c_gender": "m",
27
"c_name": {
28
"c_first": "Richard",
29
"c_last": "Roberts"
30
},
31
"fullName": "Richard Roberts"
32
}
33
],
34
"hasMore": false,
35
"object": "list"
36
}
Copied!

$skip

The skip stage skips over the specified number of documents and passes the remaining documents into the next stage of the pipeline.
1
{"$skip": <positive integer>}
Copied!

Example

Example Request
Example Response
1
GET /c_patients?pipeline=[{"$match": {"c_gender": "f"}},{"$project": {"c_name": 1}},{"$skip": 1},{"$limit":1}]
Copied!
1
{
2
"data": [
3
{
4
"_id": "59496035bfd25d0100661faf",
5
"c_name": {
6
"c_first": "Holly",
7
"c_last": "Forrester"
8
}
9
}
10
],
11
"hasMore": false,
12
"object": "list"
13
}
Copied!

$sort

The sort stage take all documents it receives and passes them into the next stage in the specified sort order.
1
{"$sort": {<property1>: <sort order>, <property2>: <sort order>, ...}}
Copied!
Where sort order can be 1 for ascending or -1 for descending.

Performance Considerations

To optimize performance, it is best to use $sort at the beginning of the pipeline or near the beginning, after a $match and $limit. And $sort should always proceed $project, $unwind, or $group if it is required -- otherwise your query may be slow or time out.

Example

Example Request
Example Response
1
GET /c_patients?pipeline=[{"$match": {"c_gender": "f"}},{"$limit": 2},{"$sort": {"c_name.c_last": 1},{"$project": {"c_name": 1}}}]
Copied!
1
{
2
"data": [
3
{
4
"_id": "59496035bfd25d0100661faf",
5
"c_name": {
6
"c_first": "Holly",
7
"c_last": "Forrester"
8
}
9
},
10
{
11
"_id": "59495fc1514ef1010000598d",
12
"c_name": {
13
"c_first": "Jane",
14
"c_last": "Smith"
15
}
16
}
17
],
18
"hasMore": false,
19
"object": "list"
20
}
Copied!

$unwind

Unwinding arrays and document arrays expands them into individual documents so that aggregation can take place of the resulting output. For example, mapping the roles array of an account with 3 roles results in 3 output documents, each containing a roles ObjectId property.
1
{"$unwind": <property>}
Copied!

Example

1
GET /accounts/?pipeline=[{"$project": {"roles": 1}}, {"$unwind": "roles"}]
2
3
{
4
"data": [
5
{
6
"_id": "55ff9db67a00a89c14aa31a6",
7
"object": "account",
8
"roles": "000000000000000000000004"
9
},
10
{
11
"_id": "55ff9db67a00a89c14aa31a6",
12
"object": "account",
13
"roles": "000000000000000000000005"
14
},
15
{
16
"_id": "55ff9db67a00a89c14aa31a6",
17
"object": "account",
18
"roles": "000000000000000000000007"
19
}
20
],
21
"hasMore": false,
22
"object": "list"
23
}
Copied!
Last modified 1mo ago