Replace One
AI-generatedOverview
This node performs various MongoDB operations on specified collections within a database. It connects to a MongoDB instance using provided credentials and executes commands such as finding documents, updating, inserting, replacing, aggregating, or deleting documents based on user-defined queries.
The Replace One operation specifically replaces a single document in a collection that matches a given query with a new document. This is useful when you want to completely overwrite an existing document rather than update specific fields.
Common scenarios:
- Replacing outdated or incorrect records with updated versions.
- Overwriting configuration documents or settings stored in MongoDB.
- Migrating data by replacing documents with new schema formats.
Example:
You have a collection of user profiles and want to replace the profile of a user identified by their email address with a new profile document.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection where the operation will be performed. |
| Query (JSON Format) | A JSON-formatted MongoDB query object used to find the document to replace. |
| Document (JSON Format) | The new document data that will replace the matched document entirely in the collection. |
Output
The output is a JSON array where each element corresponds to the replaced document(s). For the Replace One operation, the output contains the new document(s) that replaced the old ones.
json: An array of objects representing the replaced documents.- No binary data output is produced by this node.
Dependencies
- Requires a valid connection to a MongoDB database.
- Needs credentials for authenticating with the MongoDB instance (e.g., connection string and database name).
- The node uses MongoDB's native driver methods like
replaceOneto perform operations.
Troubleshooting
Common issues:
- Malformed JSON in the "Query" or "Document" properties can cause parsing errors.
- If no document matches the query, no replacement occurs; ensure the query correctly identifies the target document.
- Connection failures due to incorrect credentials or network issues.
Error messages:
- JSON parsing errors: Check that the input JSON for queries and documents is valid.
- MongoDB connection errors: Verify credentials and network accessibility.
- Operation failures: Ensure the query matches at least one document and the document structure is valid.
To handle errors gracefully, enable the node’s "Continue On Fail" option to allow workflow execution to proceed even if some operations fail.
Links and References
- MongoDB replaceOne() method
- MongoDB Query Documents
- n8n Documentation on MongoDB Node (general reference)