Overview
This node provides advanced MongoDB operations for finding, inserting, and updating documents within a specified MongoDB collection. It supports multiple operations such as aggregate queries, find, insert, update, delete, and specialized update operations like "findOneAndReplace" and "findOneAndUpdate". This flexibility makes it useful in scenarios where you need to interact programmatically with MongoDB databases directly from n8n workflows.
Typical use cases include:
- Updating existing documents based on a key field.
- Inserting new documents or upserting (update or insert) if no match is found.
- Running aggregation pipelines to transform or summarize data.
- Deleting documents matching specific criteria.
- Finding documents with sorting, limiting, and skipping options.
For example, you could use this node to update user profiles in a MongoDB collection by matching on a user ID, or to run an aggregation pipeline that calculates sales totals grouped by region.
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 (e.g., "id" or "_id"). |
| Fields | Comma-separated list of fields to include in the new document when updating or replacing. |
| Upsert | Boolean flag indicating whether to insert a new document if no matching document is found during update. |
| Options | Additional options controlling behavior: |
| - Date Fields | Comma-separated list of fields to parse as MongoDB Date type. |
| - Use Dot Notation | Whether to interpret date fields using dot notation for nested properties. |
| - _id as ObjectId | Whether to automatically convert the _id field to a MongoDB ObjectId type (default true). |
Output
The output is a JSON array representing the results of the operation:
- For find and aggregate operations, it returns an array of documents matching the query.
- For insert, it returns the inserted documents augmented with their generated IDs.
- For update, findOneAndUpdate, and findOneAndReplace, it returns the updated documents.
- For delete, it returns an object with the count of deleted documents.
- If errors occur and "Continue On Fail" is enabled, error messages are included in the output per item.
Binary data is not handled or output by this node.
Dependencies
- Requires a valid connection to a MongoDB database, configured via credentials providing either a connection string or parameters to build one.
- Uses the official MongoDB Node.js driver internally.
- Requires the target MongoDB database and collection to exist.
- No additional external services are needed beyond MongoDB itself.
Troubleshooting
- Connection Errors: Ensure the MongoDB connection string or parameters are correct and the database is accessible.
- Invalid Queries: JSON parsing errors can occur if the query or sort parameters are malformed JSON strings.
- ObjectId Conversion: If the
_idfield is used as the update key but contains invalid ObjectId strings, conversion will fail. Disable automatic ObjectId conversion or ensure valid IDs. - Upsert Behavior: When upsert is enabled, documents may be inserted if no match is found; verify this is intended.
- Field Parsing: Incorrectly specifying date fields or dot notation options may cause unexpected behavior in date handling.
- Unsupported Operations: Using an unsupported operation name will throw an error unless "Continue On Fail" is enabled.