Overview
This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports a variety of operations on MongoDB collections 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. It allows users to specify the update data, control whether to create the document if it doesn't exist (upsert), and configure other options like debug mode and query timeout.
Common scenarios:
- Updating user profile information in a "users" collection.
- Modifying a single order status in an "orders" collection.
- Adjusting configuration settings stored as documents in a MongoDB collection.
Practical example:
You want to update the email address of a user whose username is "johndoe" in the "users" collection. You provide a query to find the user by username and specify the new email in the update data. The node performs the update on that single matching document.
Properties
| Name | Meaning |
|---|---|
| Database | The name of the MongoDB database to use. Leave empty to use the default database from the configured credential. |
| Collection | The name of the MongoDB collection on which to perform the operation (e.g., "users"). This is required. |
| Query | A JSON object representing the MongoDB query filter to select the document to update. For example, {"username": "johndoe"}. |
| Schema Definition | A JSON object defining the Mongoose schema for the collection. This describes the fields and their types, e.g., {"name": "String", "email": "String", "age": "Number"}. This is required to construct the model. |
| Update Data | A JSON object containing the fields and values to update in the matched document. For example, {"email": "newemail@example.com"}. |
| Options | A collection of optional parameters controlling the update behavior: - Upsert: Whether to create the document if it does not exist ( true or false). - Multi: Whether to update multiple documents (not applicable here). - Limit, Skip, Sort, Select Fields: Not applicable for this operation. - Query Timeout (Ms): Maximum time allowed for the query to run (1-300 seconds). - Debug Mode: Enable Mongoose debug mode to log queries to console. |
| Include Query Info | Boolean flag indicating whether to include raw MongoDB query information in the response. |
Output
The output is an array of items where each item corresponds to one input item processed. Each output item contains a json field with the result of the update operation.
For the "Update One" operation, the json output includes:
- The result object returned by Mongoose's
updateOne()method, typically containing:acknowledged: Boolean indicating if the update was acknowledged.matchedCount: Number of documents matched by the query.modifiedCount: Number of documents actually modified.upsertedId: If upsert occurred, the ID of the inserted document.
If the "Include Query Info" option is enabled, additional metadata about the operation, query, update data, and options used will be included in the output under keys like operation, query, updateData, and options.
If an error occurs and "Continue On Fail" is enabled, the output item will contain an error field describing the issue.
Dependencies
- Requires a valid connection to a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library to interact with MongoDB.
- Requires credentials configured in n8n that provide access to the MongoDB server.
- Supports multi-database connections by specifying the database name or using the default from credentials.
- No external APIs beyond MongoDB are needed.
Troubleshooting
Common issues:
- Connection errors: If the node cannot connect to MongoDB, errors like "MongoDB connection error" or "connection refused" may appear. Verify the connection string, network accessibility, and that MongoDB is running.
- Authentication failures: Errors mentioning authentication failure indicate incorrect username/password or authSource settings.
- Invalid JSON: The query, update data, or schema definition must be valid JSON. Invalid JSON strings will cause parsing errors.
- Timeouts: Queries taking longer than the specified timeout (default 30 seconds) will fail.
- Large result sets warning: Although not directly relevant to updateOne, other operations warn when returning large datasets.
Error messages and resolutions:
"MongoDB connection error: ..."— Check your connection string and ensure MongoDB is reachable."MongoDB authentication failed: ..."— Verify credentials and authentication database."MongoDB host not found: ..."— Confirm hostname/IP and DNS resolution."MongoDB connection refused: ..."— Ensure MongoDB service is running and listening on the correct port."Invalid JSON format: ..."— Correct the JSON syntax in the input fields."Limit exceeds maximum allowed limit..."— For operations supporting limits, reduce the requested limit.
Links and References
- Mongoose Documentation — For schema definitions and query options.
- MongoDB Update Operators — To understand how to structure update data.
- MongoDB Query Documents — For building query filters.
- n8n Documentation — General guidance on using nodes and credentials.