Overview
This node allows interaction with a MongoDB database to perform various operations such as finding, inserting, updating, replacing, and deleting documents within specified collections. It is useful for automating workflows that require data manipulation in MongoDB, such as syncing data between systems, updating records based on external triggers, or querying data for further processing.
The "Find And Replace" operation specifically enables replacing an existing document in a collection by matching a key field (commonly the document's ID) and substituting it entirely with new data. This is beneficial when you want to overwrite a document completely rather than partially updating fields.
Practical example:
- Replacing a user profile document in a "users" collection when a complete update of the user's information is received from another system.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection where documents will be found and replaced. |
| Update Key | The property name used to identify which documents to replace. Usually this is "id" or "_id". |
| Fields | Comma-separated list of fields to include in the replacement document. |
| Upsert | Boolean flag indicating whether to insert a new document if no matching document is found. |
| Options | Additional options for the operation: |
| - 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. |
| - 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 find-and-replace operation. Each object corresponds to one input item processed and contains the updated document fields.
If errors occur during processing and the node is configured to continue on failure, the output items will contain an error field describing the issue.
No binary data output is produced by this operation.
Dependencies
- Requires a MongoDB database connection with appropriate credentials.
- Needs a valid connection string or connection parameters to connect to the MongoDB instance.
- Uses BSON types like
ObjectIdandDatefor proper data typing. - Requires n8n credentials configured for MongoDB access.
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 query parameters causing parsing errors.
- Insufficient permissions for the MongoDB user to perform update or replace operations.
Error messages:
"Database \"<name>\" does not exist": Check the database name in credentials."The operation \"findOneAndReplace\" is not supported!": Ensure the operation parameter is correctly set.- BSON conversion errors when
_idor other special fields are not properly formatted: Verify that IDs are valid ObjectId strings or use the provided options to specify OID fields. - Connection failures: Validate the connection string and network accessibility.
To resolve errors, verify all input parameters, ensure correct MongoDB credentials, and check the formatting of fields especially for dates and ObjectIds.