Mongo Db Operations icon

Mongo Db Operations

MongoDB Operations

Overview

This node performs various MongoDB operations on specified collections within a database. It is useful for automating data manipulation tasks such as querying, aggregating, updating, inserting, replacing, and deleting documents in MongoDB directly from an n8n workflow.

Common scenarios include:

  • Aggregating data with complex pipelines to generate reports or summaries.
  • Querying documents with filters, sorting, skipping, and limiting results.
  • Updating specific documents based on query criteria.
  • Inserting multiple new documents at once.
  • Replacing entire documents matching a query.
  • Deleting documents selectively.

For example, the Aggregate operation allows you to run aggregation pipelines with optional sorting, skipping, and limiting, enabling advanced data transformations and analytics within your workflows.

Properties

Name Meaning
Collection The name of the MongoDB collection on which the operation will be performed.
Query (JSON Format) A JSON object defining the MongoDB query or aggregation pipeline stages, depending on the operation. For aggregate, this is the pipeline array.
Options Additional options for the query or aggregation:
- Limit Maximum number of documents to return (0 means no limit).
- Skip Number of documents to skip before returning results.
- Sort (JSON Format) JSON object specifying the sort order of the result set, e.g., { "field": -1 } for descending sort by "field".

Output

The output is a JSON array where each item corresponds to a document or result from the MongoDB operation:

  • For Aggregate operation, the output JSON contains a single field documents, which is an array of documents resulting from the aggregation pipeline.

    Example output structure for Aggregate:

    {
      "documents": [
        { /* first aggregated document */ },
        { /* second aggregated document */ },
        ...
      ]
    }
    
  • Other operations produce outputs relevant to their action, such as updated documents, inserted documents with their IDs, or success indicators.

The node does not output binary data.

Dependencies

  • Requires a MongoDB connection configured via credentials that provide the connection string and database name.
  • Uses the official MongoDB Node.js driver internally to connect and perform operations.
  • The node expects valid JSON input for queries, updates, and aggregation pipelines.

Troubleshooting

  • Invalid JSON in Query or Pipeline: If the JSON provided in the "Query" or "Sort" fields is malformed, the node will throw a parsing error. Ensure all JSON inputs are correctly formatted.
  • Connection Issues: Failure to connect to MongoDB usually indicates incorrect credentials or network issues. Verify the connection string and database access.
  • Operation Errors: Errors during operations (e.g., invalid update syntax, non-existent collections) will cause the node to fail unless "Continue On Fail" is enabled, in which case errors are returned in the output JSON.
  • Empty Results: If queries or aggregations return empty arrays, verify that the query conditions and pipeline stages match existing data.
  • Large Result Sets: Using very high limits or no limit may impact performance; use the "Limit" option wisely.

Links and References

Discussion