MongocampAggregationBuilder
Build and run an aggregation pipeline on a collection without leaving the UI: group and count, compute fields, sort and limit — then page through the result and export it. Two views of the same pipeline: a Stages editor and a plain JSON view.
Usage
<template>
<MongocampAggregationBuilder collection-name="myCollection" />
</template>The playground has it at /secured/admin/collections/<name>/aggregate, linked from the collection's data and info pages.
Props
| Prop | Type | Description |
|---|---|---|
collectionName | string | Required. Collection the pipeline runs on. Changing it clears the shown result. |
filter | string | Optional Lucene filter (what a table shows). The server applies it as the first $match of every run — stage previews and the result page — sent in the request body (any length). It is shown as a chip Applies to: … with a remove button that drops it for the next runs. |
matchCase | boolean | Match the filter's wildcards case-sensitively (default false: filterCaseInsensitive is sent with a wildcard filter, also by the stage previews). MongocampCollectionData's aggregate link carries ?matchCase=true when the table matches case. |
The two views
Stages — a list of stages, each the JSON text of its value. Add a stage by type ($match, $group, $sort, $project, $limit, $unwind, $addFields, $count, $bucketAuto), move it up or down, or remove it. A new stage starts with a sensible example ($group → { "_id": "$field", "count": { "$sum": 1 } }). The text is kept as typed, so a half-written value is never lost.
| Stage | Value | Notes |
|---|---|---|
$match | an object | filter documents (MongoDB query syntax, not Lucene) |
$group | an object with _id | { "_id": "$field", "n": { "$sum": 1 } }; "_id": null groups everything |
$sort | an object of 1 / -1 | { "n": -1 } |
$project | an object | pick or compute fields |
$limit | a whole number ≥ 1 | a typed "10" is sent as the number 10 |
$unwind | a field path, or an object with path | "$tags" — one document per array item; { "path": "$tags", "preserveNullAndEmptyArrays": true } keeps documents without items |
$addFields | an object | { "double": { "$multiply": ["$price", 2] } } — add computed fields |
$count | the name of the result field | "total" — no $, no dot; the result is one document { "total": 12 } |
$bucketAuto | an object with groupBy and a whole buckets ≥ 1 | { "groupBy": "$price", "buckets": 5, "output": { "n": { "$sum": 1 } } } — ranges chosen by the server |
A stage with nothing in it ({} or empty) is left out of the request.
JSON — the pipeline as the usual array, [ { "$match": { … } }, { "$limit": 10 } ]. Any stage the server understands can be typed here, including ones the stage editor has no form for ($sample, $facet, $set, …).
Switching is guarded in both directions: a stage with a mistake blocks leaving the stage editor, and a pipeline using a stage the editor has no form for keeps you in the JSON view. Both say why.
Mistakes are caught before the server sees them
Live, under the stage or the JSON: not valid JSON; a stage value that is not an object; a $limit that is not a whole number of 1 or more; a $sort direction other than 1/-1; a $group without _id; in the JSON view also "not an array", "more than 50 stages", and a stage that is not an object with exactly one $-named key (the message names the stage number). Run stays disabled while there is any mistake.
What is not allowed, and why
These operators are refused anywhere in a pipeline — as a stage or nested inside another stage's value (a $function hides inside $addFields, a $merge inside a $facet). The list is the default deny-list of MongoCamp server 2.0 (AGGREGATION_STAGES_DENIED):
| Operator | Why |
|---|---|
$function, $accumulator, $where | run JavaScript on the database server |
$out, $merge | write to collections |
$currentOp, $listSessions | show what the database server is doing for everybody |
$collStats, $indexStats | their rows name the database server; index usage comes from GET …/index/stats instead |
The server refuses these itself as well (403, code 4031), even when nested; if one ever got past the check above, the alert reads "This pipeline uses $function, which is not allowed here." instead of a permission error.
$lookup, $unionWith and $graphLookup are allowed: the server checks the caller's read grant for every collection they read and refuses one without a grant (403, code 4032).
MongoCamp's users
When a pipeline reads mc_users — run on it, or joining it with $lookup / $graphLookup / $unionWith — the password hash and the API key never appear: password and apiKey are removed from every result row at any depth, and a pipeline that refers to them by path ($project: { x: '$password' }, $group: { _id: '$user.apiKey' }) is refused with "This pipeline reads … a hidden field of MongoCamp's users". The same holds for the stage previews and the charts.
The refusal names the operator (and the stage in the JSON view). The same check runs again in useMongocampAggregation().runAggregation, so nothing that bypasses this component can send such a pipeline either. Only keys count: a string value or field name such as "$out" or merge is just data.
Preview after a stage
Each stage has Preview after this stage: it runs the stages up to and including that one, cut by a $limit of 5 and without a count (count=none), and shows those documents right under the stage — so you see what a $group or $unwind really produced before adding the next one. It runs when opened and again, debounced (500 ms), when you edit any stage it depends on; an older request is cancelled. A closed preview makes no request. A stage with a mistake blocks the preview of itself and of every later stage until it is fixed, and a server error is shown with MongoDB's own message. The preview never replaces the real result.
Templates
Templates adds the stages for a common question; every stage stays editable:
| Template | Stages |
|---|---|
| Count by field | $group by the field with a count, $sort by the count |
| Most common values | the same, then $limit (10 by default, 1–1000) |
| Documents per year, month or day | $group by $dateToString of a date field (%Y, %Y-%m or %Y-%m-%d), $sort by the period |
| Sum and average by group | $group by a field with total, avg and count of a number field, $sort by the group |
The field names of the collection are offered while typing (the server's field list; if it cannot be read you can still type). The stages go after the stages that are already there; an editor that still holds only the untouched first stage is replaced. Field names must not contain $.
Saved pipelines
Saved pipelines keeps named pipelines of this collection in this browser only (50 per collection, useMongocampSavedPipelines). Save current pipeline stores what would run (it must be valid and not empty); clicking a saved one puts it into the stage editor, or into the JSON view when it uses a stage the editor has no form for. A stored pipeline is checked like one typed into the JSON view, so edited browser storage cannot bring back a denied operator.
Running and results
Run sends the pipeline and shows the first page.
- Paging is done by the server: the page-size select (10 / 20 / 50 / 100, default 20) and the pager re-run the pipeline that produced the result, not whatever has been typed since. Changing the page size goes back to page 1. A run that is overtaken by a newer one is ignored.
- The table has one column per key found in the first rows,
_idfirst (a$group's key). Objects and arrays show as compact JSON with the full text as a tooltip; very long values are shortened. - The header shows the total number of results. Export (download icon) saves the current page as JSON or CSV — raise the page size first for more.
- A pipeline with no results shows an empty state, not an error.
- Results arrive as relaxed Extended JSON and are shown as plain values: ids as hex text, dates as ISO text, and whole numbers beyond 2^53 as text with every digit (the response is read from its text, because the generated client's
JSON.parsewould round them) — a note under the table says so.
Charting a result
The Table | Chart switch above the result charts the page already on screen with MongocampAggregationChart — no extra request; paging re-runs the pipeline as before and the chart follows the new page.
When it fails
A failed run shows an alert with the reason and Retry (or Log in again when the session has expired). For a command MongoDB rejects, only MongoDB's own message is shown — the server's raw error also contains the address of the database server, which is never displayed. A request blocked on its way to the server (an intermittent 405 was seen) is reported as retryable.
Not included
Sharing pipelines between browsers, charts, and stages beyond the nine forms (use the JSON view). Pipelines are read-only by design — nothing here writes data.
Related
useMongocampAggregation— the calls behind it- Strings live under
nuxtUiMongocamp.aggregation.*in the English and German locale files