Executes an aggregation pipeline. The collection.aggregate() method has the following parameter.

Parameter Type Description
pipeline array A sequence of data aggregation operations or stages. See the aggregation pipeline operators for more information.
Returns:A cursor to the documents produced by the final stage of the aggregation pipeline operation.


Unsupported Aggregation Stages

The following aggregation stages are not supported:

  • $collStats
  • $currentOp
  • $lookup
  • $out
  • $indexStats
  • $facet
  • $graphLookup
  • $text
  • $geoNear

Cursor Handling

A cursor cannot be returned to a client but can be iterated inside a MongoDB Stitch function. The cursor returned by collection.aggregate() supports the following methods:

Method Description Iterates and returns a single document from the cursor or null if none remain
cursor.toArray() Iterates the cursor to exhaustion and returns all iterated documents in an array

Working with BSON

MongoDB actions may return documents containing BSON types.

Assume the collection test.example contains the following document.

  "_id": ObjectId("5a0c516ac7ecd33f1aea0915"),
  "binary_data": BinData(0,"ymz="),
  "owner_id": "5b1574258f25b9240ed8f8bb"

When this document is retrieved in Stitch, _id is represented by a BSON.ObjectId type and binary_data is represented as a BSON.Binary type.

The following example shows how a MongoDB Stitch function can manipulate these types.

exports = function() {
  const mongodb ="mongodb-atlas");
  const collection = mongodb.db("test").collection("example");

  const pipeline = [
    { "$match": {
    } }
  return collection
    .then(docs => => {
      const asBase64 = doc.binary_data.toBase64();
      const asOidString = doc._id.toString();
      return { asBase64, asOidString }

For more information on manipulating BSON in Stitch, see BSON.


The following examples assume that the test.example namespace has been added to MongoDB in the Stitch admin console and appropriate rules have been set. See MongoDB Rules for more information.

Executing a Pipeline

Assume the test.example collection contains the following documents:

{ "grade": 87, "_id": ObjectId("5a0c516ac7ecd33f1aea0915") }
{ "grade": 89, "_id": ObjectId("5a0c516ac7ecd33f1aea0916") }
{ "grade": 62, "_id": ObjectId("5a0c516ac7ecd33f1aea0917") }
{ "grade": 22, "_id": ObjectId("5a0c516ac7ecd33f1aea0918") }
{ "grade": 68, "_id": ObjectId("5a0c516ac7ecd33f1aea0919") }
{ "grade": 91, "_id": ObjectId("5a0c516ac7ecd33f1aea091A") }

MongoDB Stitch contains a function named find_grades with the following definition:

exports = function() {
    const mongodb ="mongodb-atlas");
    const collection = mongodb.db("test").collection("example");
    const cursor = collection.aggregate([
      {"$project": {"curved_grade": {"$add": ["$grade", 9]}}},
      {"$match": {"curved_grade": {"$gt": 80}}},
      {"$sort": {"curved_grade": -1}}
    return cursor.toArray();

Calling this function returns the following:

{ "curved_grade": 100, "_id": ObjectId("5a0c516ac7ecd33f1aea091A") }
{ "curved_grade": 98, "_id": ObjectId("5a0c516ac7ecd33f1aea0916") }
{ "curved_grade": 96, "_id": ObjectId("5a0c516ac7ecd33f1aea0915") }