Skip to content

Aggregation ​

POST /mongodb/collections/{name}/aggregate takes stages as {"stage": "match", "value": {...}}. The stage name works with and without $:

json
{
  "pipeline": [
    { "stage": "match", "value": { "status": "open" } },
    { "stage": "group", "value": { "_id": "$customer", "total": { "$sum": "$amount" } } },
    { "stage": "sort", "value": { "total": -1 } }
  ]
}

Refused operators and collections ​

RuleStatusx-error-code
An operator of AGGREGATION_STAGES_DENIED anywhere in the pipeline (stage, nested pipeline, expression)4034031
A collection read by $lookup.from, $graphLookup.from or $unionWith (also nested) without the read grant4034032
A stage that reads another database (from: {"db": ..., "coll": ...})4034032

The default denied list is $function, $accumulator, $where (JavaScript on the database server), $out, $merge (writes), and $currentOp, $listSessions, $collStats, $indexStats (they expose server addresses). Use GET .../index/stats for index usage.

$lookup, $unionWith and $graphLookup read collections in the database of the request's collection (geodata:locations with from: "accounts" reads geodata:accounts).

A UI that wants to hide refused stages can read the effective list from GET /system/capabilities (aggregationStagesDenied).

With a Lucene filter ​

POST .../aggregate?filter= (or filter in the request body, for long filters; not both: 400, code 4004) puts the Lucene filter in front of the pipeline as a $match stage. The aggregation then works on exactly the documents the list shows, and the database can use an index.

A pipeline that must start with a special stage ($geoNear, $search, $searchMeta, $vectorSearch, $documents) cannot get a stage in front of it: with a filter it answers 400 (code 4005). Put the conditions into the stage itself in that case.

Local time ​

The route has no time zone parameter. Use the timezone option of the date operators instead. A histogram per day in Berlin time:

json
[
  { "stage": "group", "value": { "_id": { "$dateToString": { "format": "%Y-%m-%d", "date": "$createdAt", "timezone": "Europe/Berlin" } }, "n": { "$sum": 1 } } },
  { "stage": "sort", "value": { "_id": 1 } }
]

A named zone follows daylight saving time, an offset (+02:00) does not.

Released under the MIT License.