Navigation

collection.aggregate()

Definition

collection.aggregate(pipeline)

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.

Behavior

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
cursor.next() 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 service actions may return documents containing BSON types.

Assume the collection test.example contains the following document.

{ "_id": ObjectId("5a0c516ac7ecd33f1aea0915"), "binary_data": BinData(0,"ymz=") }

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() {
    var coll = context.services.get("mongodb-atlas").db("test").collection("example");
    var doc = coll.aggregate([]).next();
    var asBase64 = doc.binary_data.toBase64();
    var asOidString = doc._id.toString();
    return [asBase64, asOidString];
}

For more information on manipulating BSON in Stitch, see :ref :bson-in-stitch.

Examples

The following examples assume that the test.example namespace has been added to the MongoDB service and appropriate rules have been set. See MongoDB Service 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() {
    var mongodb = context.services.get("mongodb-atlas");
    var coll = mongodb.db("test").collection("example");
    var cursor = coll.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") }

See also

collection.aggregate()