MongoDBNg icon

MongoDBNg

Nextgen Find, insert and update documents in MongoDB

Overview

This node provides advanced MongoDB operations including finding, inserting, updating, replacing, and deleting documents within a specified MongoDB collection. It is designed for users who want to interact with MongoDB databases directly from their n8n workflows without writing custom code.

The "Find And Replace" operation specifically allows you to find documents based on a key and replace them entirely with new data. This is useful when you need to update whole documents atomically, ensuring the document is replaced rather than partially updated.

Common scenarios:

  • Replacing outdated or incorrect records in a database with fresh data.
  • Synchronizing external data sources by replacing existing entries.
  • Performing atomic updates where partial updates are insufficient.

Example:
You have a collection of user profiles and want to replace a user's entire profile document identified by their unique ID with a new profile object.

Properties

Name Meaning
Collection The name of the MongoDB collection where the operation will be performed.
Update Key The property name used to identify which documents to update or replace. Typically this is "id" or "_id".
Fields Comma-separated list of fields to include in the new document during replacement.
Upsert Boolean flag indicating whether to insert a new document if no matching document is found (true = insert if not 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.
- _id as ObjectId: Automatically convert _id field to MongoDB ObjectId type (default true).

Output

The output is an array of JSON objects representing the documents affected by the operation:

  • For "Find And Replace", the output contains the replaced documents after the operation.
  • Each item corresponds to one input item processed.
  • If errors occur and "Continue On Fail" is enabled, error messages are included in the output JSON under an error property.
  • The node does not output binary data.

Dependencies

  • Requires a valid connection to a MongoDB database.
  • Needs credentials providing access to the MongoDB instance (e.g., connection string or parameters).
  • Uses the official MongoDB Node.js driver internally.
  • No additional external services are required.

Troubleshooting

  • Invalid Collection Name: Ensure the collection name exists in the target database.
  • Invalid Update Key: The update key must exist in the input data; otherwise, no documents will match.
  • ObjectId Conversion Errors: If _id values are not valid ObjectId strings but conversion is enabled, errors may occur. Disable automatic ObjectId conversion if your IDs are strings.
  • Malformed JSON in Fields: The Fields property expects comma-separated field names; invalid formatting can cause failures.
  • Upsert Behavior: If upsert is false and no matching document is found, no changes occur.
  • Connection Issues: Verify that the MongoDB credentials and network connectivity are correct.
  • Error Messages: Errors thrown during execution include descriptive messages such as "Database does not exist," "Invalid ObjectId," or MongoDB driver errors. Enabling "Continue On Fail" allows processing to continue despite errors, with error details returned in output.

Links and References

Discussion