- Stitch >
- MongoDB Atlas >
- Work With MongoDB
Run Aggregation Pipelines¶
On this page
Overview¶
The code snippets on this page demonstrate how configure and run aggregation pipelines against a collection. Aggregation operations run all documents in a collection through a series of data aggregation stages that allow you to filter and shape documents as well as collect summary data about groups of related documents.
Supported Aggregation Stages
Stitch supports nearly all MongoDB aggregation pipeline stages and operators, but some stages must be executed within a system function. See Aggregation Framework Limitations for more information.
Data Model¶
The examples on this page use a collection named store.purchases
that contains information about historical item sales in an online
store. Each document contains a list of the purchased items
,
including the item name
and the purchased quantity
, as well as a
unique ID value for the customer that purchased the items.
Snippet Setup¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
To use a code snippet in a function, you must first instantiate a MongoDB collection handle:
To use a code snippet in a JavaScript project, you must first do the following:
Import Stitch Dependencies¶
– or –
Instantiate a MongoDB Remote Collection Handle¶
To use a code snippet in an Android project, you must first do the following:
Set Up Your Project¶
Follow the steps in the Set up a MongoDB Mobile Project guide.
Note
For more details on setting up your Android app to use Stitch, refer to Build a Mobile App with Sync or Build a Local-Only Mobile App.
Import Stitch Dependencies¶
For CRUD operations on a remote MongoDB collection, you will use one
or more of the following import
statements:
To Sync documents between a remote and a local MongoDB instance, you will also need to import the following packages:
Important
If you use Sync, be sure to add .sync()
before every remote
database call in the examples below, as in the following:
To perform CRUD operations only on the local (on-device) database, import the following packages:
Instantiate a MongoDB Collection Handle¶
To use a code snippet in an iOS project, you must first do the following:
Set Up Your Project¶
Follow the steps in the Set up a MongoDB Mobile Project guide.
Import Stitch Dependencies¶
Initialize the MongoDB Stitch iOS SDK¶
Instantiate a MongoDB Collection Handle¶
Methods¶
Aggregate Documents in a Collection¶
You can execute an aggregation pipeline using the
collection.aggregate()
action.
The following snippet groups all documents in the purchases
collection by their customerId
value and aggregates a count of the
number of items each customer purchases as well as the total number of
purchases that they made. After grouping the documents the pipeline adds
a new field that calculates the average number of items each customer
purchases at a time, averageNumItemsPurchased
, to each customer’s
document:
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
Aggregation Stages¶
Filter Documents¶
You can use the $match stage to filter incoming documents according to a standard query filter.
Example
The following $match
stage filters incoming documents to include
only those where the graduation_year
field has a value between
2019
and 2024
, inclusive.
Group Documents¶
You can use the $group stage to aggregate summary
data for groups of one or more documents. MongoDB groups documents based
on the _id
expression.
Note
You can reference a specific document field by prefixing the field
name with a $
.
Example
The following $group
stage groups documents by the value of their
customerId
field and calculates the number of purchase documents
that each customerId
appears in.
Project Document Fields¶
You can use the $project stage to include or omit
specific fields from documents or to calculate new fields using
aggregation operators.
To include a field, set its value to 1
. To omit a field, set its
value to 0
.
Note
You cannot simultaneously omit and include fields other than _id
.
If you explicitly include a field other than _id
, any fields you
did not explicitly include are automatically omitted (and
vice-versa).
Example
The following $project
stage omits the _id
field, includes
the customerId
field, and creates a new field named numItems
where the value is the number of documents in the items
array:
Add Fields to Documents¶
You can use the $addFields stage to add new fields with calculated values using aggregation operators.
Note
$addFields
is similar to $project but does not allow you to
include or omit fields.
Example
The following $addFields
stages creates a new field named
numItems
where the value is the number of documents in the
items
array:
Unwind Array Values¶
You can use the $unwind stage to aggregate individual elements of array fields. When you unwind an array field, MongoDB copies each document once for each element of the array field but replaces the array value with the array element in each copy.
Example
The following $unwind
stage creates a new document for each
element of the items
array in each document. It also adds a field
called itemIndex
to each new document that specifies the
element’s position index in the original array:
Consider the following document from the purchases
collection:
If we apply the example $unwind
stage to this document, the stage
outputs the following three documents: