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 such as updating documents within a specified collection. The "Update" operation allows users to modify existing documents based on a key property, optionally inserting new documents if no match is found (upsert). This is useful for synchronizing data, maintaining up-to-date records, or modifying specific fields in bulk.

Practical examples include:

  • Updating user profiles in a MongoDB collection where the user ID matches.
  • Modifying product details in an inventory database by specifying product codes.
  • Conditionally inserting new documents if they do not already exist, ensuring data completeness.

Properties

Name Meaning
Collection The name of the MongoDB collection where documents will be updated.
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 Whether to insert a new document if no existing document matches the update key (true or false).
Options Additional options including:
- Date Fields Comma-separated list of fields to parse as MongoDB Date type.
- Use Dot Notation Whether to use dot notation to access nested date fields (true or false).
- OID Fields Comma-separated list of fields to parse as MongoDB ObjectId type.

Output

The output is an array of JSON objects representing the documents after the update operation. Each object corresponds to an input item processed by the node and includes the updated fields.

If errors occur during processing individual items and the node is configured to continue on failure, the output for those items will contain an error field with the error message.

No binary data output is produced by this operation.

Dependencies

  • Requires a connection to a MongoDB database, authenticated via an API key credential or similar authentication method.
  • The node depends on the MongoDB Node.js driver and BSON utilities for handling ObjectId and Date types.
  • Proper configuration of the MongoDB credentials and connection string is necessary within n8n.

Troubleshooting

  • Common Issues:

    • Incorrect collection name or database name can cause connection or query failures.
    • Malformed JSON in the "Fields" or other parameters may lead to parsing errors.
    • Using an invalid or missing update key will prevent correct document matching.
    • Improperly formatted ObjectId or Date fields can cause type conversion errors.
  • Error Messages:

    • "Database \"<name>\" does not exist": Verify the database name in credentials.
    • Errors related to ObjectId conversion usually indicate invalid IDs; ensure IDs are valid hexadecimal strings.
    • "The operation \"update\" is not supported!": Indicates an unsupported operation was selected; verify the operation parameter.
    • Connection errors suggest issues with network, credentials, or MongoDB server availability.
  • Resolutions:

    • Double-check all input parameters for correctness.
    • Validate that the MongoDB server is accessible and credentials are correct.
    • Use the "Continue On Fail" option to handle partial failures gracefully.

Links and References

Discussion