Mongo DB OID icon

Mongo DB OID

Find, insert and update documents in MongoDB

Overview

This node allows users to interact with a MongoDB database by performing various operations such as finding, inserting, updating, deleting, and aggregating documents within specified collections. The "Find And Replace" operation specifically enables replacing entire documents in a collection based on a key field, which is useful for synchronizing or updating records precisely.

Common scenarios include:

  • Replacing outdated or incorrect documents in a MongoDB collection with updated versions.
  • Synchronizing data from external sources by matching on a unique key and replacing the existing document.
  • Performing bulk replacements of documents where partial updates are insufficient.

For example, you might use this node to replace user profile documents in a "users" collection by matching on a user ID and providing a complete new document structure.

Properties

Name Meaning
Collection The name of the MongoDB collection to operate on.
Update Key The property name used to identify which documents to update or replace (e.g., "id").
Fields Comma-separated list of fields to include in the new document when replacing.
Upsert Boolean indicating whether to insert a new document if no matching document is found.
Options Additional options:
- Date Fields Comma-separated list of fields that should be parsed as MongoDB Date types.
- Use Dot Notation Whether to use dot notation to access nested date fields.

Output

The output is an array of JSON objects representing the documents after the find-and-replace operation. Each object corresponds to one replaced document, containing the fields specified in the input along with any MongoDB-generated identifiers.

If errors occur during processing individual items and the node is configured to continue on failure, the output will include error messages within the respective item’s JSON.

No binary data output is produced by this operation.

Dependencies

  • Requires a connection to a MongoDB database, authenticated via an API key credential or connection string.
  • The node depends on the MongoDB Node.js driver and BSON utilities for handling ObjectId and extended JSON parsing.
  • Proper configuration of the MongoDB credentials in n8n is necessary, including specifying the target database and connection details.

Troubleshooting

  • Common Issues:

    • Incorrect collection name or database can cause failures.
    • Malformed JSON in query or fields parameters may lead to parsing errors.
    • Using an invalid update key that does not exist in documents will result in no matches.
    • Attempting to replace documents without required permissions in MongoDB.
  • Error Messages:

    • "Database \"<name>\" does not exist": Verify the database name in credentials.
    • Errors related to ObjectId conversion usually mean the update key value is not a valid MongoDB ObjectId string.
    • "The operation \"<operation>\" is not supported!": Indicates an unsupported operation was requested; ensure "Find And Replace" is selected.
  • To resolve errors, check the correctness of all input parameters, ensure the MongoDB server is accessible, and verify permissions.

Links and References

Discussion