MongoDB icon

MongoDB

Find, insert and update documents in MongoDB

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.
  • Filtering, grouping, and reshaping data before further processing in an n8n workflow.
  • Generating reports or analytics by aggregating data directly within MongoDB.

Practical example: You might use the Aggregate operation to filter orders placed after a certain date, group them by customer, and calculate total sales per customer, all within a single aggregation pipeline.

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. This defines the sequence of stages to process the data (e.g., $match, $group, $sort).

Output

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

Example output item structure:

{
  "json": {
    // Aggregated document fields as returned by MongoDB
  }
}

No binary data output is produced by this operation.

Dependencies

  • Requires a MongoDB database accessible via connection string or credentials.
  • Needs an API key credential or equivalent authentication configured in n8n to connect securely to the MongoDB instance.
  • Uses the official MongoDB Node.js driver internally to execute queries.

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 Format: If the pipeline references _id fields as strings, they are automatically converted to ObjectId instances. However, invalid ObjectId strings will cause errors.
  • Database or Collection Not Found: Errors occur if the specified database or collection does not exist or is inaccessible. Verify the connection credentials and collection names.
  • Permission Issues: Insufficient permissions on the MongoDB user may lead to authorization errors when running aggregation pipelines.
  • Continue On Fail: If enabled, the node will continue processing other items even if some fail, returning error messages in the output instead of stopping execution.

Links and References

Discussion