Actions11
Overview
The node enables updating documents in a MongoDB collection. It is designed to modify existing records based on a specified key, with options to insert new documents if no match is found (upsert). This operation is useful when you want to keep your database synchronized with external data sources or update specific fields of documents dynamically.
Typical use cases include:
- Updating user profiles or product information where the unique identifier (e.g.,
id) determines which document to update. - Synchronizing data from APIs or other databases by matching on a key and updating relevant fields.
- Conditionally inserting new documents if they do not already exist, using the upsert option.
For example, you might update a customer's address in a "customers" collection by specifying the customer ID as the update key and providing the new address fields.
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 (commonly "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 update 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. |
Output
The output consists of an array of JSON objects representing the updated documents. Each output item corresponds to an input item processed by the node.
- For each updated document, the output JSON contains the fields that were updated.
- If an error occurs during the update of a particular item and the node is configured to continue on failure, the output JSON for that item will contain an
errorfield with the error message. - The output preserves pairing with the input items to maintain traceability.
No binary data is produced by this operation.
Dependencies
- Requires a MongoDB instance accessible via connection string or parameters.
- Needs an API authentication token or credentials to connect securely to the MongoDB server.
- The node depends on the official MongoDB Node.js driver for database operations.
- Proper configuration of the MongoDB credentials within n8n is necessary.
Troubleshooting
Common Issues:
- Incorrect collection name or database name can cause errors like "collection does not exist."
- Invalid update key or missing fields may result in no documents being updated.
- Malformed JSON in input data or fields list can cause parsing errors.
- Using
_idas update key requires the value to be a valid MongoDB ObjectId string; otherwise, conversion errors occur.
Error Messages:
"Database \"<name>\" does not exist": Verify the database name in credentials."error": "<message>"in output JSON: Indicates an error occurred for that specific item. Check the message for details.- Connection failures: Ensure network connectivity and correct authentication credentials.
Resolutions:
- Double-check collection and database names.
- Validate input data format and ensure required fields are present.
- When using
_idas update key, provide valid ObjectId strings. - Enable "Continue On Fail" to process all items even if some fail, allowing partial success.