MongoDB Pro icon

MongoDB Pro

Find, insert and update documents in MongoDB

Overview

The node provides advanced interaction with MongoDB databases, allowing users to perform various operations on documents within specified collections. The Aggregate operation specifically enables running MongoDB aggregation pipeline queries, which are powerful for data processing and transformation tasks such as filtering, grouping, sorting, and reshaping documents.

This node is beneficial when you need to perform complex data aggregations directly in MongoDB without retrieving all documents and processing them externally. For example, you can calculate statistics, generate reports, or transform data structures efficiently using the aggregation framework.

Practical example:

  • Aggregate sales data by month to calculate total revenue per month.
  • Filter documents created after a certain date and group them by category.
  • Perform multi-stage transformations like lookup (join), unwind arrays, and project specific fields.

Properties

Name Meaning
Collection The name of the MongoDB collection where the aggregation query will be executed.
Query The MongoDB aggregation pipeline expressed as a JSON array. This defines the stages of the aggregation process.

The Query property expects a valid MongoDB aggregation pipeline, e.g.:

[
  { "$match": { "date": { "$gt": "1950-01-01" } } },
  { "$group": { "_id": "$category", "total": { "$sum": "$amount" } } }
]

Output

The output consists of an array of items, each containing a json field representing a single document resulting from the aggregation pipeline. Each document corresponds to one aggregated result from MongoDB.

Example output item:

{
  "json": {
    "_id": "someCategory",
    "total": 12345
  }
}

If errors occur during execution and the node is configured to continue on failure, the output item will contain an error field describing the issue.

No binary data output is produced by this operation.

Dependencies

  • Requires a MongoDB database accessible via connection string or parameters.
  • Needs an API key credential or equivalent authentication token configured in n8n to connect securely to the MongoDB instance.
  • The node uses the official MongoDB Node.js driver internally.

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 and properly formatted.
  • Invalid ObjectId: If the query contains _id fields as strings, they are automatically converted to ObjectId instances. However, invalid ObjectId strings will cause errors.
  • Database or Collection Not Found: Errors may occur if the specified database or collection does not exist or the credentials lack access permissions.
  • Aggregation Pipeline Errors: MongoDB may return errors if the pipeline stages are invalid or unsupported. Review the MongoDB aggregation documentation to verify pipeline correctness.
  • Connection Issues: Network problems or incorrect credentials will prevent connecting to MongoDB. Verify connection details and network accessibility.

To resolve errors, check the error messages returned in the output or logs, validate your aggregation pipeline, and confirm database connectivity.

Links and References

Discussion