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 integrating MongoDB data manipulation directly into n8n workflows, enabling automation of database queries, updates, inserts, replacements, and deletions without writing custom code.

Common scenarios include:

  • Retrieving documents matching specific criteria with sorting, skipping, and limiting options.
  • Updating or replacing documents based on query filters.
  • Inserting multiple new documents at once.
  • Deleting documents matching certain conditions.
  • Running aggregation pipelines to process and transform data.

For example, you could use this node to find all users born after 1950, update their status, insert new records from an external source, or delete outdated entries automatically.

Properties

Name Meaning
Collection The name of the MongoDB collection to operate on.
Query (JSON Format) A JSON object defining the MongoDB query filter to select documents. Used in find, update, replaceOne, deleteOne, and aggregate operations. Example: { "birth": { "$gt": "1950-01-01" } }
Options Additional query options available for find and aggregate operations:
- Limit Maximum number of documents to return (use 0 for unlimited). Default is 50.
- Skip Number of documents to skip in the result set. Default is 0.
- Sort (JSON Format) JSON object defining the sort order of results. Example: { "field": -1 } for descending by "field".

Output

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

  • For Find: Each output item contains one document matching the query.
  • For Aggregate: Outputs a single item with a documents field containing an array of aggregated documents.
  • For Update: Outputs success status, modified count (1 if updated, 0 otherwise), and the updated document.
  • For Insert: Outputs each inserted document including its generated _id.
  • For ReplaceOne: Outputs the replaced document.
  • For DeleteOne: Outputs the original input items (no direct confirmation).

No binary data output is produced by this node.

Dependencies

  • Requires a valid MongoDB connection string and database name provided via credentials.
  • Needs network access to the MongoDB server.
  • No additional external services are required beyond MongoDB itself.

Troubleshooting

  • Invalid JSON in Query or Options: The node expects JSON strings for queries and options. Malformed JSON will cause parsing errors. Ensure proper JSON formatting.
  • Connection Errors: If the node cannot connect to MongoDB, verify the connection string, network accessibility, and credentials.
  • Empty Results: If no documents match the query, the output will be empty. Check your query filters and collection names.
  • Operation Failures: Errors during update, insert, or delete operations will throw exceptions unless "Continue On Fail" is enabled, in which case error details are returned in the output.
  • Sorting Issues: The sort option must be a valid JSON object with field names and sort directions (1 or -1).

Links and References

Discussion