Mongo DB OID icon

Mongo DB OID

Find, insert and update documents in MongoDB

Overview

This node enables interaction with a MongoDB database, supporting operations such as finding, inserting, updating, deleting, and aggregating documents within specified collections. The "Find And Update" operation specifically allows users to locate documents based on a key field and update them, optionally inserting new documents if no match is found (upsert). This is useful in scenarios where you want to synchronize data, maintain up-to-date records, or modify existing entries conditionally.

Practical examples:

  • Updating user profiles in a MongoDB collection by matching on a unique user ID.
  • Replacing or updating product information in an inventory database.
  • Conditionally inserting new documents if they do not already exist, ensuring data consistency.

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 (e.g., "id").
Fields Comma-separated list of fields to include in the updated document.
Upsert Boolean flag indicating whether to insert a new document if no existing document matches the update key.
Options Additional options for the operation:
- Date Fields Comma-separated list of fields that should be parsed as MongoDB Date types.
- Use Dot Notation Whether to use dot notation to access nested date fields.
- OID Fields Comma-separated list of fields that should be parsed as MongoDB ObjectId types.

Output

The output is a JSON array representing the documents after the find-and-update operation:

  • Each item corresponds to a processed input document, reflecting the updated state.
  • If errors occur during processing individual items and the node is configured to continue on failure, the error message will be included in the json field of the respective item.
  • For insertions or updates, the output includes the updated document fields.
  • The node does not output binary data.

Dependencies

  • Requires a MongoDB database connection, authenticated via an API key credential or equivalent authentication token configured in n8n.
  • Uses the official MongoDB Node.js driver and BSON utilities for handling ObjectId and Date types.
  • Requires proper configuration of the MongoDB credentials in n8n, including connection string or parameters specifying the target database.

Troubleshooting

  • Common issues:

    • Incorrect collection name or database configuration leading to connection failures.
    • Malformed query or update fields causing MongoDB errors.
    • Using an invalid or missing update key field resulting in no documents being matched.
    • Improper formatting of date or ObjectId fields causing parsing errors.
  • Error messages:

    • "Database \"<name>\" does not exist": Indicates the specified database is not found; verify database name and credentials.
    • MongoDB driver errors related to invalid ObjectId or date formats: Ensure fields listed under "Date Fields" and "OID Fields" are correctly formatted and present.
    • "The operation \"<operation>\" is not supported!": Occurs if an unsupported operation is selected; verify the operation parameter.
  • Resolution tips:

    • Double-check all input parameters, especially collection names and update keys.
    • Validate that date and ObjectId fields are correctly specified and formatted.
    • Enable "Continue On Fail" to handle partial failures gracefully during batch operations.

Links and References

Discussion