Mongo DB OID icon

Mongo DB OID

Find, insert and update documents in MongoDB

Overview

This node enables interaction with a MongoDB database, specifically supporting operations to find and update documents within a specified collection. The "Find And Update" operation allows users to locate documents based on a key field and update them accordingly. It supports upserting (inserting if no matching document is found), selective field updates, and handling of special data types like dates and ObjectIDs.

Common scenarios include:

  • Updating user profiles or records in a MongoDB collection where the unique identifier (e.g., id or _id) determines which documents to update.
  • Synchronizing external data sources by updating existing entries or inserting new ones if they don't exist.
  • Managing content or configuration stored in MongoDB by selectively updating fields.

Example use case:

  • A workflow receives updated customer information and uses this node to update the corresponding MongoDB documents based on the customer's unique ID. If a customer does not exist, it can insert a new record automatically.

Properties

Name Meaning
Collection The name of the MongoDB collection to operate on.
Update Key The property name used to identify which documents to update. Commonly "id" or "_id".
Fields Comma-separated list of fields to include in the update document.
Upsert Boolean flag indicating whether to insert a new document if no matching document is found (true or false).
Options Additional options for the operation:
- Date Fields Comma-separated list of fields that should be parsed as MongoDB Date type.
- Use Dot Notation Whether to use dot notation to access nested date fields (true or false).

Output

The output is an array of JSON objects representing the documents after the update operation:

  • For each input item processed, the output contains the updated document fields.
  • If the operation involves insertion (upsert), the inserted document's ID is included as id.
  • In case of errors during processing individual items, the error message is included in the item's JSON under the error key.
  • No binary data output is produced by this node.

Dependencies

  • Requires a MongoDB instance accessible via connection string or configured credentials.
  • Needs an API authentication token or credentials to connect securely to the MongoDB database.
  • Uses the official MongoDB Node.js driver and BSON utilities internally.
  • Requires proper n8n credential setup for MongoDB connections.

Troubleshooting

  • Common issues:

    • Incorrect collection name or database name leading to "collection not found" errors.
    • Invalid or missing update key causing no documents to match for update.
    • Malformed JSON in fields or query parameters causing parsing errors.
    • Attempting to update documents without required permissions.
    • Using string IDs instead of ObjectId types when _id is the update key.
  • Error messages and resolutions:

    • "Database \"<name>\" does not exist": Verify the database name in credentials and ensure the database exists.
    • "The operation \"<operation>\" is not supported!": Confirm the selected operation is valid; only supported operations are allowed.
    • Errors related to ObjectId conversion: Ensure that _id fields are provided as valid ObjectId strings or converted properly.
    • Connection failures: Check network connectivity and credentials validity.
  • Enabling "Continue On Fail" allows the node to process all items even if some fail, returning error details per item.

Links and References

Discussion