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. 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).
Common scenarios where this node is beneficial include:
- Synchronizing data from external sources by updating existing records or adding new ones.
- Maintaining up-to-date records in a MongoDB collection based on incoming workflow data.
- Automating database maintenance tasks like bulk updates or conditional replacements.
Practical example:
- A workflow receives customer data updates and uses this node to find customers by their unique ID and update their contact information. If a customer does not exist, the node can insert a new record automatically.
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 | Boolean flag indicating whether to insert a new document if no matching document is found. |
Options | Additional options for the update operation: |
- ID Fields | Comma-separated list of fields that should be parsed as MongoDB ObjectId type. |
- Date Fields | Comma-separated list of fields that should be parsed as MongoDB Date type. |
- Use Dot Notation | Whether to use dot notation when accessing date fields (boolean). |
Output
The output consists of an array of JSON objects representing the documents after the update operation. Each item corresponds to one input item processed, containing either the updated document data or an error message if the operation failed for that item.
- For successful updates, the output JSON reflects the updated document fields.
- If an error occurs during processing and the node is set to continue on failure, the output JSON contains an
error
field with the error message. - No binary data is produced by this operation.
Dependencies
- Requires a MongoDB database connection configured via an API key credential or connection string.
- The node depends on the official MongoDB Node.js driver for database interactions.
- Proper permissions on the MongoDB server are necessary to perform update and insert operations.
- n8n credentials must be set up with valid MongoDB access details.
Troubleshooting
Common issues:
- Incorrect collection name or database name causing "collection not found" errors.
- Invalid or missing update key leading to no documents being matched.
- Malformed JSON in fields or options parameters causing parsing errors.
- Insufficient permissions for update or insert operations on the MongoDB server.
- Using string IDs without proper conversion to ObjectId may cause no matches.
Error messages and resolutions:
"Database \"<name>\" does not exist"
: Verify the database name in credentials."error": "<message>"
in output JSON: Check the specific error message for details; often related to invalid query syntax or permission issues.- Connection failures: Ensure network connectivity and correct authentication credentials.
- To resolve ObjectId issues, specify the ID fields in the "ID Fields" option so they are correctly parsed.