Update Document
Updates one or more documents in a MongoDB collection that match a specified filter.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
- MongoDB Client Id - The client ID returned from the Connect node (optional if credentials are provided).
- Database Name - The name of the database containing the collection.
- Collection Name - The name of the collection to update documents in.
- MongoDB Query - JSON object containing two fields:
- Filter - Query filter to match documents for update
- Update - Update operations to apply to matched documents
Options
- Credentials - Database credentials (Category 5) - optional if using Client ID from Connect node. This allows you to perform the operation without a separate Connect node.
Output
- Result - The number of documents that were successfully modified.
How It Works
The Update Document node modifies documents in a MongoDB collection based on a filter and update specification. When executed, the node:
- Validates that database name and collection name are not empty
- Obtains a MongoDB client (either from client ID or by creating one from credentials)
- Processes the MongoDB Query input with Handlebars template rendering
- Parses the rendered JSON into Filter and Update components
- Validates that both Filter and Update are provided
- Accesses the specified collection
- Executes UpdateMany operation with the provided filter and update
- Returns the count of modified documents
Requirements
- Either a valid client ID from Connect node OR database credentials
- Valid database name (non-empty)
- Valid collection name (non-empty)
- Valid JSON with both Filter and Update fields
- Appropriate permissions to update documents in the collection
Error Handling
The node will return specific errors in the following cases:
- ErrInvalidArg - Database name, collection name, Filter, or Update is empty/invalid, or client/credentials are invalid
- ErrConnection - Cannot connect to MongoDB (when using credentials)
- JSON parsing errors if the query format is invalid
- Update operator errors if update syntax is incorrect
- Permission errors if the user doesn't have update rights
- Collection not found errors if the collection doesn't exist
Usage Notes
- Uses UpdateMany operation, which updates all documents matching the filter
- Returns the number of documents actually modified (not just matched)
- If no documents match the filter, the result will be 0
- The MongoDB Query field supports Handlebars templates for dynamic values
- Must use MongoDB update operators like $set, $inc, $push, etc.
- You can use either the client ID approach or direct credentials approach
- Documents matched but not changed (already have target values) are not counted as modified
Query Format
The MongoDB Query must contain both Filter and Update:
{
"Filter": {
"field": "value"
},
"Update": {
"$set": {
"field": "new_value"
}
}
}
Update Operators
Common MongoDB update operators:
- $set - Sets the value of a field
- $unset - Removes a field
- $inc - Increments a numeric field
- $mul - Multiplies a numeric field
- $rename - Renames a field
- $currentDate - Sets field to current date
- $min - Updates field if specified value is less than current
- $max - Updates field if specified value is greater than current
- $push - Adds item to array
- $pull - Removes item from array
- $addToSet - Adds item to array if not already present
- $pop - Removes first or last item from array
Example Usage
Scenario 1: Update user status
- Connect node → Client Id
- Update Document:
- MongoDB Client Id: (from Connect)
- Database Name: "myapp"
- Collection Name: "users"
- MongoDB Query:
{
"Filter": {
"email": "john@example.com"
},
"Update": {
"$set": {
"status": "active",
"last_login": "2024-01-15"
}
}
} - Output: updated_count
Scenario 2: Increment product stock
Update Document:
- Database Name: "inventory"
- Collection Name: "products"
- Credentials: (select credential)
- MongoDB Query:
{
"Filter": {
"sku": "PROD-001"
},
"Update": {
"$inc": {
"stock": 50
}
}
}
- Output: result
Scenario 3: Update with dynamic values using Handlebars
Set Variables:
- order_id = "ORD-12345"
- new_status = "shipped"
- tracking_number = "TRACK-999"
Update Document:
- Database Name: "orders"
- Collection Name: "sales"
- MongoDB Query:
{
"Filter": {
"order_id": "{{order_id}}"
},
"Update": {
"$set": {
"status": "{{new_status}}",
"tracking": "{{tracking_number}}",
"shipped_date": "{{current_date}}"
}
}
}
Scenario 4: Update multiple fields with different operators
Update Document:
- Database Name: "analytics"
- Collection Name: "page_views"
- MongoDB Query:
{
"Filter": {
"page": "/home"
},
"Update": {
"$inc": {
"views": 1
},
"$set": {
"last_viewed": "2024-01-15T10:30:00Z"
},
"$push": {
"visitors": "192.168.1.1"
}
}
}
Scenario 5: Bulk status update
Update Document:
- Database Name: "tasks"
- Collection Name: "todos"
- MongoDB Query:
{
"Filter": {
"status": "pending",
"due_date": {
"$lt": "2024-01-01"
}
},
"Update": {
"$set": {
"status": "overdue"
}
}
}
- Output: overdue_count
Log: "Marked " + overdue_count + " tasks as overdue"
Scenario 6: Add tags to documents
Update Document:
- Database Name: "blog"
- Collection Name: "posts"
- MongoDB Query:
{
"Filter": {
"category": "technology"
},
"Update": {
"$addToSet": {
"tags": "tech"
}
}
}
Scenario 7: Rename field across documents
Update Document:
- Database Name: "legacy"
- Collection Name: "records"
- MongoDB Query:
{
"Filter": {},
"Update": {
"$rename": {
"old_field_name": "new_field_name"
}
}
}
Common Use Cases
- Status Updates: Change document status or state
- Inventory Management: Adjust stock levels and quantities
- User Management: Update user profiles, preferences, or settings
- Order Processing: Update order status and tracking information
- Analytics: Increment counters and update metrics
- Data Correction: Fix errors or update outdated information
- Migration: Modify document structure during schema changes
- Timestamp Updates: Track last modified or accessed times
Filter Examples
Update single document by ID:
{
"Filter": {
"_id": {
"$oid": "507f1f77bcf86cd799439011"
}
},
"Update": {...}
}
Update documents matching multiple conditions:
{
"Filter": {
"status": "pending",
"priority": "high",
"created_date": {
"$gte": "2024-01-01"
}
},
"Update": {...}
}
Update all documents:
{
"Filter": {},
"Update": {...}
}
Best Practices
- Always test your filter with Read Document first to verify which documents will be updated
- Use specific filters to avoid unintended updates
- Include timestamp fields to track when updates occur
- Log the modified count to track changes
- Use $set operator for most field updates to avoid replacing entire documents
- Consider using Read-Modify-Write pattern for complex updates
- Use transactions when updates must be coordinated with other operations
- Implement validation before updates to ensure data integrity
Common Errors
Empty Database Name:
- Cause: Database name input is empty
- Solution: Provide a valid database name
Empty Collection Name:
- Cause: Collection name input is empty
- Solution: Provide a valid collection name
Invalid Filter:
- Cause: Filter field is missing or null in the MongoDB Query
- Solution: Ensure MongoDB Query contains a valid "Filter" field
Invalid Update:
- Cause: Update field is missing or null in the MongoDB Query
- Solution: Ensure MongoDB Query contains a valid "Update" field with update operators
Invalid Update Operator:
- Cause: Using incorrect or non-existent update operator
- Solution: Use valid MongoDB update operators ($set, $inc, $push, etc.)
Modifying _id Field:
- Cause: Attempting to change the _id field
- Solution: MongoDB doesn't allow _id modification; create a new document instead
Permission Denied:
- Cause: User doesn't have update permission
- Solution: Ensure user has readWrite or dbAdmin role
Performance Tips
- Create indexes on fields used in Filter for better performance
- Avoid updating large numbers of documents in a single operation
- Consider batch processing for bulk updates
- Monitor update operations for performance bottlenecks
- Use projection to limit fields returned if needed
Related Nodes
- Insert Document - Add new documents
- Delete Document - Remove documents
- Read Document - Query documents before updating
- Read All - Retrieve all documents from a collection