Navigation

aggregate

On this page

Definition

aggregate

Executes a MongoDB aggregation pipeline on a collection and returns the results.

Important

In order to use MongoDB service actions, you must have rules set up for each namespace included in the operation.

You can only use the aggregate action in the first stage of a pipeline. The aggregate action stage has the following syntax:

{
  "service":"mongodb-atlas",
  "action":"aggregate",
  "args":{
     "database": <dbname>,
     "collection": <collection name>,
     "pipeline":[<aggregation stage1>, ...]
  }
}

The aggregate action takes the following arguments:

Argument Type Description
database string Name of the database.
collection string Name of the collection.
pipeline array The aggregation pipeline stages.

Example

Consider the following pipeline that performs an aggregation pipeline on the specified collection:

[{
   "service":"mongodb-atlas",
   "action":"aggregate",
   "args":{
      "database":"my_db",
      "collection":"orders",
      "pipeline":[
         {"$group":{"_id":{"cust_id":"$customer","item":"$item"},"total":{"$sum":"$qty"}}},
         {"$out":"reports"}
      ]
   }
}]

Stage1: The MongoDB aggregate stage first groups the documents by the customer and item fields and then writes the grouped documents into the my_db.reports collection.