Overview
This node enables interaction with a MongoDB database, allowing users to perform various operations such as aggregation, querying, inserting, updating, and deleting documents within specified collections. The "Aggregate" operation specifically executes a MongoDB aggregation pipeline query, which is a powerful framework for data aggregation modeled on the concept of data processing pipelines.
Common scenarios where this node is beneficial include:
- Performing complex data transformations and computations on MongoDB collections without writing custom code.
- Generating reports or summaries by grouping and filtering data using aggregation stages.
- Enriching or reshaping data before passing it downstream in an n8n workflow.
For example, you might use the Aggregate operation to filter documents created after a certain date, group them by a category field, and calculate totals or averages.
Properties
Name | Meaning |
---|---|
Collection | The name of the MongoDB collection on which the aggregation pipeline will be executed. |
Query | The MongoDB aggregation pipeline expressed as a JSON array of stages. Each stage defines a step in the data processing pipeline. Example: [{"$match": {"date": {"$gt": "1950-01-01"}}}, ...] . |
Output
The output is a JSON array where each element corresponds to a document resulting from the aggregation pipeline execution. Each document reflects the transformed data as defined by the aggregation stages.
If any errors occur during execution (e.g., invalid query syntax), the node can output an error object instead if configured to continue on failure.
No binary data output is produced by this operation.
Dependencies
- Requires a connection to a MongoDB database, authenticated via credentials that provide access to the target database.
- The node depends on the MongoDB Node.js driver and BSON utilities for parsing and handling ObjectId types.
- Proper configuration of the MongoDB credentials in n8n is necessary, including either a connection string or individual connection parameters.
Troubleshooting
- Invalid Aggregation Pipeline Syntax: If the JSON query is malformed or contains invalid aggregation stages, the node will throw an error. Ensure the pipeline is valid according to MongoDB's aggregation framework syntax.
- ObjectId Parsing Issues: If the pipeline references
_id
fields as strings, the node attempts to convert them to ObjectId instances. Incorrect formats may cause errors. - Database or Collection Not Found: Errors will occur if the specified database or collection does not exist or the credentials lack sufficient permissions.
- Connection Failures: Network issues or incorrect credentials will prevent connecting to MongoDB.
- To handle errors gracefully, enable the node’s "Continue On Fail" option to receive error details in the output rather than stopping the workflow.