Overview
This node performs update operations on MongoDB collections using Extended JSON (EJSON) format for queries and update data. It supports updating multiple documents or a single document with options to upsert (insert if no match found), and control over the returned document state (before or after update). The node is useful when you want to modify existing records in MongoDB with complex queries and updates expressed in EJSON, which allows handling of BSON types like ObjectId and Date.
Common scenarios:
- Updating user profiles or order statuses based on specific criteria.
- Replacing or modifying documents atomically with findOneAndReplace or findOneAndUpdate.
- Conditionally inserting new documents if none match the update query (upsert).
- Returning either the original or updated document after modification.
Practical example:
- Update all documents in the "orders" collection where status is "pending" to set status to "processed" and add an updated timestamp.
- Find a document by its
_idand replace it entirely with a new document, returning the updated version.
Properties
| Name | Meaning |
|---|---|
| Collection | The MongoDB collection name where the update operation will be performed. |
| Update Key | The property name used to identify which documents to update, typically _id. |
| Update Query (EJSON Format) | A MongoDB query in EJSON format to select documents to update (used in update and findOneAndUpdate). |
| Update Data (EJSON Format) | The update instructions in EJSON format specifying how to modify the matched documents. Required for update and findOneAndUpdate. |
| Upsert | Boolean flag indicating whether to insert a new document if no matching documents are found. |
| Lean Output | Whether to return plain JavaScript objects without MongoDB-specific ObjectId wrappers. |
| Options | Additional options for the update operation: |
| - Return Updated Document | For findOneAndUpdate and findOneAndReplace, choose whether to return the document before or after the update (before or after). |
Output
The output is an array of JSON objects representing the result of the update operation(s):
- For bulk updates (
update), output includes counts such asmatchedCount,modifiedCount,upsertedCount, and theupsertedIdif applicable. - For
findOneAndUpdateandfindOneAndReplace, output contains the document either before or after the update depending on the option selected. - If
leanOutputis enabled, MongoDB ObjectId fields are converted to plain strings or removed for easier consumption. - In case of errors per item, the output JSON will contain an
errorfield describing the issue.
No binary data output is produced by this node.
Dependencies
- Requires a MongoDB database connection configured via credentials that provide access to the target database.
- Uses MongoDB Node.js driver and BSON EJSON parsing to handle queries and updates.
- No additional external services beyond MongoDB are required.
Troubleshooting
Invalid EJSON query or update data:
Error message:Invalid EJSON query: ...orInvalid EJSON: ...
Resolution: Ensure the query and update JSON strings are valid EJSON. Use correct syntax for special BSON types like ObjectId ({"$oid": "..."}) and Date ({"$date": "..."}).Database or collection not found:
Error may occur if the specified database or collection does not exist or credentials lack permissions.
Resolution: Verify database and collection names, and ensure credentials have proper access rights.No documents matched for update and upsert disabled:
The update operation will not modify any documents if no matches are found and upsert is false.
Resolution: Enable upsert if insertion of new documents is desired when no matches exist.Incorrect use of update key:
If the update key is_id, ensure values are valid ObjectId strings; otherwise, the update may fail or not match documents.
Resolution: Provide valid ObjectId strings or change the update key to another unique field.Error during update operation:
Errors thrown from MongoDB driver will be surfaced. Use the error messages to diagnose issues such as invalid operators or malformed queries.