MongoDB Pro icon

MongoDB Pro

Find, insert and update documents in MongoDB

Overview

The "Find And Update" operation of the MongoDB Pro node allows users to locate documents in a specified MongoDB collection based on a key field and update them with new data. It supports conditional updates where the document to be updated is identified by a specific property (commonly an ID). Additionally, it can perform an upsert operation, meaning if no matching document is found, it can insert a new one instead.

This operation is beneficial in scenarios such as synchronizing data between systems, updating user profiles, or modifying records based on external triggers. For example, when a user's information changes in an external CRM, this node can find the corresponding MongoDB document by user ID and update the relevant fields accordingly.

Properties

Name Meaning
Collection The name of the MongoDB collection where documents will be searched and 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 Boolean flag indicating whether to insert a new document if no existing document matches the update key.
Options Additional options:
- 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.

Output

The output consists of JSON objects representing the documents after the update operation. Each output item corresponds to an input item processed, enriched with metadata linking back to the original input.

  • For each updated document, the output JSON contains the updated fields.
  • If an error occurs during processing an item and "continue on fail" is enabled, the output for that item will contain an error field with the error message.
  • No binary data is produced by this operation.

Dependencies

  • Requires a valid connection to a MongoDB database, authenticated via an API key credential or similar authentication token configured in n8n.
  • The node depends on the official MongoDB Node.js driver for database operations.
  • Proper configuration of the MongoDB connection string and database name is necessary.

Troubleshooting

  • Common Issues:

    • Incorrect collection name or database name leading to "collection not found" errors.
    • Invalid update key property causing no documents to match.
    • Malformed field lists or date fields causing parsing errors.
    • Insufficient permissions for update or insert operations in MongoDB.
  • Error Messages:

    • "Database \"<name>\" does not exist": Verify the database name in credentials.
    • "error": "<message>" in output JSON: Indicates an error occurred for that particular item; check the message for details.
    • Connection failures: Ensure network connectivity and correct authentication credentials.
  • Resolutions:

    • Double-check collection and database names.
    • Confirm the update key exists in the documents.
    • Validate the format of fields and date fields inputs.
    • Ensure the MongoDB user has write permissions.

Links and References

Discussion