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 a single document, finding documents, creating new documents, deleting documents, counting documents, and running aggregation pipelines. The "Update One" operation specifically updates a single document in a specified collection based on a query filter.
Typical use cases include:
- Updating user profiles or records in a MongoDB collection.
- Modifying configuration or settings stored in MongoDB.
- Applying partial updates to documents without replacing entire entries.
- Ensuring atomic update operations on single documents.
For example, you might use this node to update the email address of a user identified by their username, or to increment a counter field in a specific document.
Properties
| Name | Meaning |
|---|---|
| Database | Name of the MongoDB database to connect to. Leave empty to use the default database from credentials. |
| Collection | Name of the MongoDB collection where the update will be performed. |
| Query | MongoDB query object (in JSON format) used to find the document 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. |
| Options | Additional options for the update operation: |
| - Limit | Maximum number of results to return (not applicable for updateOne). |
| - Skip | Number of documents to skip (not applicable for updateOne). |
| - Sort | Sort order for matching documents (not applicable for updateOne). |
| - Select Fields | Fields to include or exclude in the result (not applicable for updateOne). |
| - Upsert | Whether to create a new document if no matching document is found. |
| - Multi | Whether to update multiple documents (only relevant for update, not updateOne). |
| - Query Timeout (Ms) | Maximum time allowed for the query to run, in milliseconds (1-300 seconds). |
| - Debug Mode | Enables Mongoose debug mode to log all MongoDB queries to the console. |
| Include Query Info | Boolean flag to include raw MongoDB query information in the response. |
Output
The output is an array of JSON objects representing the result of the update operation. For the "Update One" operation, the output JSON typically contains:
acknowledged: Boolean indicating if the update was acknowledged by MongoDB.matchedCount: Number of documents matched by the query (should be 0 or 1).modifiedCount: Number of documents actually modified (0 or 1).upsertedId: If upsert was enabled and a new document was created, this contains the ID of the new document.
If the "Include Query Info" option is enabled, the output also includes metadata about the operation, such as the original query, update data, and options used.
No binary data output is produced by this node.
Dependencies
- Requires a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library for schema definition and database operations.
- Requires an API key credential or connection credentials configured in n8n to authenticate with MongoDB.
- Supports multi-database connections by allowing specification of the database name.
Troubleshooting
- Connection Errors: Errors like "MongoDB connection error", "host not found", or "connection refused" indicate issues with the connection string, network, or MongoDB server availability. Verify the connection details and ensure MongoDB is running.
- Authentication Failures: Messages about authentication failure suggest incorrect username, password, or authentication source settings. Double-check credentials.
- Invalid JSON Format: Input properties such as Query, Update Data, or Schema Definition must be valid JSON. Invalid JSON will cause parsing errors.
- Limit Exceeded: Setting a limit above the maximum allowed (10,000) will throw an error for safety reasons.
- Large Result Sets Warning: When retrieving large numbers of documents (e.g., >5000), a warning is logged suggesting adding indexes or refining filters.
- Schema Mismatch: Incorrect schema definitions may cause unexpected behavior; ensure the schema matches the collection structure.
- Timeouts: Queries taking longer than the specified timeout (default 30 seconds) will fail. Adjust the timeout or optimize queries accordingly.