MongoDB icon

MongoDB

Find, insert and update documents in MongoDB

Overview

This node enables interaction with a MongoDB database to perform various data operations such as finding, inserting, updating, replacing, and deleting documents within specified collections. It is particularly useful for automating workflows that require dynamic data manipulation in MongoDB without writing custom code.

Common scenarios include:

  • Querying documents based on complex filters.
  • Inserting new records from incoming workflow data.
  • Updating existing documents by matching keys.
  • Replacing entire documents atomically.
  • Deleting documents matching certain criteria.

For example, you could use this node to update user profiles stored in MongoDB when new information arrives, or to find and process orders matching specific conditions.

Properties

Name Meaning
Collection The name of the MongoDB collection where the operation will be performed.
Update Key The property name used to identify which documents to update or replace. Typically this is "id" or "_id".
Fields Comma-separated list of fields to include in the new document when updating or replacing.
Upsert Boolean flag indicating whether to insert a new document if no existing document matches the update key during update or replace operations.
Options Additional options for the operation:
- ID Fields: Comma-separated fields parsed as Mongo ObjectId type.
- Date Fields: Comma-separated fields parsed as Mongo Date type.
- Use Dot Notation: Whether to access nested fields using dot notation.

Output

The output consists of an array of items, each containing a json object representing the result of the MongoDB operation:

  • For find and aggregate operations, each item contains one document matching the query.
  • For insert, the output includes the inserted documents augmented with their generated IDs.
  • For update, findOneAndUpdate, and findOneAndReplace, the output reflects the updated or replaced documents.
  • For delete, the output contains the count of deleted documents.
  • If errors occur and "Continue On Fail" is enabled, error messages are included in the json.error field.

No binary data is output by this node.

Dependencies

  • Requires a valid connection to a MongoDB instance, authenticated via an API key credential configured in n8n.
  • The node uses the official MongoDB Node.js driver internally.
  • The user must provide correct connection details including database name and connection string or parameters.
  • No additional external services are required.

Troubleshooting

  • Connection Errors: Ensure the MongoDB credentials and connection string are correct and the database exists. Error messages like Database "X" does not exist indicate misconfiguration.
  • Invalid Queries: JSON parsing errors may occur if the query parameter is not valid JSON. Always provide well-formed JSON strings.
  • ObjectId Conversion: When filtering by _id, ensure the value is a valid string convertible to MongoDB ObjectId; otherwise, the query will fail.
  • Field Parsing: Incorrectly specifying ID or date fields in options can cause type conversion issues.
  • Upsert Behavior: If upsert is enabled but no document matches, a new document will be inserted. Disable upsert if this is not desired.
  • Continue On Fail: Enable this option to prevent the node from stopping execution on errors, allowing partial success.

Links and References

Discussion