Overview
This node enables performing MongoDB aggregation operations on a specified collection using an aggregation pipeline query. It is useful for transforming and analyzing data stored in MongoDB by applying multiple stages such as filtering, grouping, sorting, and projecting documents.
Common scenarios include:
- Summarizing sales data by grouping orders by month.
- Filtering records based on complex criteria.
- Joining data from multiple collections using
$lookup. - Calculating computed fields or reshaping documents.
For example, you can use this node to run an aggregation pipeline that matches documents after a certain date, groups them by category, and calculates totals.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection on which to perform the aggregation operation. |
| Query | The MongoDB aggregation pipeline expressed as a JSON array of stages (e.g., $match, $group). |
| Options | Additional options: |
| _id as ObjectId | Whether to automatically convert the _id field from string to MongoDB ObjectId type. If false, _id remains a string. |
Output
The output contains 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 the _id as ObjectId option is enabled and applicable, the _id fields in the output are converted to ObjectId types and then stringified for output consistency.
No binary data output is produced by this operation.
Dependencies
- Requires a valid connection to a MongoDB database, authenticated via an API key credential or connection string configured in n8n credentials.
- Uses the official MongoDB Node.js driver internally.
- The user must provide a valid aggregation pipeline query in JSON format.
Troubleshooting
- Invalid JSON in Query: If the aggregation pipeline JSON is malformed, the node will throw a parsing error. Ensure the JSON syntax is correct.
- Invalid Collection Name: Specifying a non-existent collection will cause errors. Verify the collection exists in the target database.
- Invalid ObjectId Conversion: If
_id as ObjectIdis enabled but the_idstring is not a valid ObjectId, conversion will fail. Disable this option or ensure_idstrings are valid ObjectIds. - Connection Issues: Errors connecting to MongoDB usually indicate incorrect credentials or network issues. Check the configured credentials and network accessibility.
- Unsupported Operation: Using an operation other than "aggregate" with this configuration will result in an error.