Aggregate
AI-generatedOverview
This node performs various MongoDB operations on specified collections within a database. It is useful for integrating MongoDB data manipulation directly into n8n workflows, enabling automation of database queries and updates without writing custom code.
The Aggregate operation specifically allows users to run aggregation pipelines on MongoDB collections. Aggregation pipelines process data records and return computed results, which is beneficial for tasks like data summarization, transformation, filtering, and grouping.
Practical examples:
- Summarizing sales data by month.
- Filtering and transforming user activity logs.
- Calculating averages or totals from large datasets.
- Combining multiple stages of data processing in one query.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection on which the operation will be performed. |
| Query | A JSON-formatted MongoDB aggregation pipeline array defining the stages of the aggregation operation. |
| Options | Additional options to modify the query behavior: |
| - Limit | Maximum number of documents to return (0 means no limit). |
| - Skip | Number of documents to skip before returning results. |
| - Sort | JSON object defining the sort order of the result set, e.g., { "field": -1 } for descending order. |
Output
The output is a JSON array where each element corresponds to a document returned by the aggregation pipeline after applying any specified options (limit, skip, sort).
Each item in the output contains the fields as returned by the MongoDB aggregation pipeline, representing processed and aggregated data according to the pipeline stages.
No binary data output is produced by this node.
Dependencies
Requires a valid connection to a MongoDB instance, including:
- A connection string to the MongoDB server.
- The target database name.
- An API key credential or authentication token configured in n8n to authorize access to the MongoDB instance.
The node depends on the official MongoDB Node.js driver for connecting and executing commands.
Troubleshooting
Common issues:
- Invalid JSON format in the Query or Sort fields can cause parsing errors.
- Incorrect collection names or database credentials will lead to connection failures.
- Using unsupported aggregation pipeline stages or syntax errors in the pipeline may cause MongoDB errors.
Error messages:
"SyntaxError: Unexpected token ..."indicates malformed JSON input; verify and correct the JSON formatting.- Connection errors suggest checking the MongoDB connection string and credentials.
- MongoDB aggregation errors usually include details about invalid pipeline stages or operators; review the aggregation pipeline accordingly.
To handle errors gracefully, enable the node's "Continue On Fail" option to allow workflow execution to proceed even if some operations fail.