Mongo DB OID icon

Mongo DB OID

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

Practical example: Suppose you want to calculate the total sales per product category from a sales collection. You can use the Aggregate operation with a pipeline that groups sales by category and sums the amounts, returning the aggregated results for downstream workflow steps.

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 for data processing (e.g., $match, $group, $sort).

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 pipeline stages.

If the aggregation pipeline produces binary data fields, those would be included in the JSON output accordingly, but the node primarily returns JSON documents representing the aggregation results.

Dependencies

  • Requires a valid connection to a MongoDB instance, authenticated via an API key credential or equivalent authentication method configured in n8n.
  • The node depends on the official MongoDB Node.js driver and BSON utilities for parsing and handling MongoDB-specific data types like ObjectId.
  • Proper configuration of the MongoDB credentials in n8n is necessary, including database name and connection string or parameters.

Troubleshooting

  • Invalid Aggregation Pipeline Syntax: If the JSON provided in the Query property is malformed or contains invalid MongoDB aggregation stages, the node will throw an error. Ensure the pipeline is correctly formatted JSON and follows MongoDB aggregation syntax.
  • ObjectId Conversion Issues: The node attempts to convert string _id fields to ObjectId instances. If the _id string is not a valid ObjectId format, errors may occur. Validate _id values or omit them if not needed.
  • Connection Errors: Failure to connect to the MongoDB instance due to incorrect credentials, network issues, or missing database will cause errors. Verify credentials and network accessibility.
  • Unsupported Operation: Using an operation other than those implemented (aggregate, find, insert, update, delete, etc.) will result in an error indicating unsupported operation.
  • Continue On Fail Behavior: If enabled, the node will return error messages in the output JSON instead of stopping execution, useful for debugging or partial failure tolerance.

Links and References

Discussion