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, running aggregation pipelines, and creating new documents within specified collections.
The Update operation specifically allows users to update one or multiple documents in a MongoDB collection based on a query filter. It supports options like upserting (creating a document if none matches the query) and controlling whether to update multiple documents or just one.
Common scenarios:
- Updating user profiles or records in a database where certain conditions are met.
- Bulk updating status fields for multiple documents matching a filter.
- Modifying specific fields in documents without replacing the entire document.
- Conditionally inserting a new document if no existing document matches the query (upsert).
Practical example:
You have a users collection and want to update the status field to "active" for all users who have logged in within the last 30 days. You can specify a query filtering those users and provide the update data { "status": "active" }. Optionally, you can enable multi-update to affect all matched documents.
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 collection within the database to operate on. |
| Query | A JSON object defining the MongoDB query filter to select documents to update. For example, { "age": { "$gte": 18 } }. Leave empty ({}) to match all documents. |
| Schema Definition | JSON definition of the Mongoose schema for the collection. Defines the expected fields and their types, e.g., { "name": "String", "age": "Number" }. This is required to create the Mongoose model dynamically. |
| Update Data | JSON object containing the fields and values to update in the matched document(s). For example, { "status": "active" }. |
| Options | Collection of additional options: - Limit: Maximum number of results to return (not applicable for update). - Skip: Number of documents to skip (not applicable for update). - Sort: Sort order (not applicable for update). - Select Fields: Fields to include/exclude (not applicable for update). - Upsert: Boolean indicating whether to insert a new document if no match is found. - Multi: Boolean indicating whether to update multiple documents (true) or only one (false). - Query Timeout (Ms): Max time allowed for the query to run. - Debug Mode: Enables Mongoose debug mode to log queries to console. |
| Include Query Info | Boolean flag to include raw MongoDB query information in the response output. Useful for debugging or logging purposes. |
Output
The node outputs an array of JSON objects corresponding to each input item processed. For the Update operation, the output JSON contains the result of the update operation, which typically includes:
acknowledged: Boolean indicating if the update was acknowledged by MongoDB.matchedCount: Number of documents matched by the query.modifiedCount: Number of documents actually modified.upsertedId: If upsert occurred, the ID of the newly created document.- Additional metadata depending on the MongoDB driver response.
If the Include Query Info option is enabled, the output also contains the original query, update data, and options used for the operation.
Example output JSON structure:
{
"acknowledged": true,
"matchedCount": 5,
"modifiedCount": 3,
"upsertedId": null,
"query": { "status": "pending" },
"updateData": { "status": "active" },
"options": { "upsert": false, "multi": true }
}
Dependencies
- Requires a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library to interact with MongoDB.
- Requires configuration of an API key credential that provides access to the MongoDB connection string and optionally default database name.
- Supports multi-database configurations allowing selection of different databases dynamically.
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 credentials, hostnames, ports, and ensure MongoDB is running.
- Authentication failures: Messages about authentication failure suggest incorrect username/password or misconfigured authentication database. Double-check credentials and authSource settings.
- Invalid JSON format: Input properties like Query, Update Data, or Schema Definition must be valid JSON. Invalid JSON will cause parsing errors.
- Limit exceeded: The node enforces a maximum limit on returned documents for safety. Exceeding this limit throws an error.
- Large result sets warning: When retrieving large numbers of documents, a warning is logged suggesting adding indexes or refining filters.
- Timeouts: Queries exceeding the configured max time will fail. Adjust the Query Timeout option if needed.
- Schema mismatch: Incorrect schema definitions may cause unexpected behavior. Ensure the schema matches the actual collection structure.