Overview
This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports a variety of operations such as updating documents, finding documents, deleting documents, counting documents, and running aggregation pipelines on specified collections within a MongoDB database.
For the Update operation specifically, the node updates one or multiple documents in a collection based on a provided query filter. It can also optionally create a new document if no matching documents are found (upsert). This is useful for scenarios where you want to modify existing data records or ensure certain data exists by updating or inserting it.
Practical examples:
- Updating user profiles in a "users" collection based on user ID or email.
- Modifying product details in an inventory collection when stock levels change.
- Applying bulk updates to multiple documents that match specific criteria.
Properties
| Name | Meaning |
|---|---|
| Database | The name of the MongoDB database to connect to. Leave empty to use the default database from credentials. |
| Collection | The name of the collection within the database to operate on. |
| Query | A JSON object defining the MongoDB query filter to select which documents to update. |
| Schema Definition | JSON definition of the Mongoose schema for the target collection, specifying field types. |
| Update Data | JSON object containing the fields and values to update in the matched document(s). |
| Options | Additional options controlling the update behavior: |
| - Limit | Maximum number of results to return (not applicable for update but available for other operations). |
| - Skip | Number of documents to skip (not applicable for update). |
| - Sort | JSON object defining sort order (not applicable for update). |
| - Select Fields | String listing fields to include/exclude (not applicable for update). |
| - Upsert | Boolean indicating whether to insert a new document if no matching document is found. |
| - Multi | Boolean indicating whether to update multiple documents (true) or just one (false). |
| - Query Timeout (Ms) | Maximum time in milliseconds allowed for the query to run (between 1000 and 300000 ms). |
| - Debug Mode | Enables Mongoose debug mode to log all MongoDB queries to the console. |
| Include Query Info | Boolean to include raw MongoDB query information in the response output. |
Output
The node outputs JSON data representing the result of the update operation:
- For update (multiple documents), the output includes the result object returned by Mongoose's
updateManymethod, typically containing fields likeacknowledged,matchedCount,modifiedCount, andupsertedIdif upsert occurred. - For updateOne, the output contains the result from Mongoose's
updateOnemethod with similar metadata about the operation. - If the option "Include Query Info" is enabled, the output JSON will also include the original query, update data, and options used for the operation.
- In case of errors, the output may contain an error message describing what went wrong.
The output is structured as an array of items, each corresponding to an input item processed, with the JSON data under the json key.
Dependencies
- Requires a valid connection to a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library for schema definition and database operations.
- Needs an API key credential or equivalent authentication configured in n8n to connect securely to MongoDB.
- Supports multi-database configurations, allowing selection of the target database dynamically.
Troubleshooting
- Connection timeout or failure: Ensure the MongoDB server is running and reachable at the specified host and port. Verify network connectivity and firewall settings.
- Authentication failed: Check username, password, and authentication database settings in your credentials.
- Invalid JSON format: Input properties like Query, Update Data, and Schema Definition must be valid JSON. Errors parsing these will cause failures.
- Limit exceeded: Although not directly relevant for update, other operations enforce a maximum limit of 10,000 documents for safety.
- Large result sets warning: When retrieving large numbers of documents (not update), consider adding indexes or refining queries to improve performance.
- Debug mode: Enable debug mode to see detailed MongoDB queries in the console, helpful for diagnosing issues.