Update Document
Document Operations
Update Document
Update an existing document with validation, versioning, and permission checks
PUT
Update Document
Overview
Update a document in a collection by its ID. The endpoint provides:- Schema validation - Ensures updates comply with schema rules
- Immutable field protection - Prevents modification of immutable fields
- Field-level write permissions - Enforces deny_write policies
- Automatic field updates - Sets
updated_atandupdated_by - Version snapshots - Saves previous version if versioning is enabled
- Pre/post-update hooks - Custom validation and side effects
- Audit logging - Tracks field-level changes
Request
Path Parameters
The name of the collection containing the document
The unique identifier (_id) of the document to update
Headers
Bearer token for authentication
Body
The request body should contain a JSON object with the fields to update. Only the fields you want to change need to be included.Fields to update. The system automatically:
- Validates against schema constraints
- Checks immutable field restrictions
- Verifies deny_write permissions
- Updates
updated_attimestamp - Sets
updated_byto current user ID
Response
Returns the complete updated document with all fields.The document’s unique identifier
ISO 8601 timestamp of the update (automatically set)
User ID who performed the update (automatically set)
All other fields from the document, including both updated and unchanged fields
Examples
Basic Update
Partial Update
Response
Implementation Details
MongoDB Query Pattern
The handler uses MongoDB’sreplaceOne operation with merged document:
Workflow
- Extract collection and ID from URL path (handlers_crud.go:276-277)
- Validate parameters - collection and ID required (handlers_crud.go:279-286)
- Authenticate request and get auth context (handlers_crud.go:289)
- Validate collection exists in schema (handlers_crud.go:296)
- Fetch existing document from MongoDB (handlers_crud.go:308)
- Check RBAC update permission on the document (handlers_crud.go:330)
- Parse update fields from JSON body (handlers_crud.go:339)
- Check deny_write fields - verify user can modify these fields (handlers_crud.go:348)
- Merge documents - combine old and new data (handlers_crud.go:361)
- Set auto fields -
updated_at,updated_by(handlers_crud.go:364-365) - Validate merged document against schema (handlers_crud.go:369)
- Check immutable fields - ensure immutable fields unchanged (handlers_crud.go:377)
- Execute pre-update hooks (handlers_crud.go:392)
- Save version snapshot if versioning enabled (handlers_crud.go:416)
- Update in MongoDB (handlers_crud.go:421)
- Execute post-update hooks (best-effort) (handlers_crud.go:430)
- Log audit event with field changes (handlers_crud.go:446)
- Fetch updated document (handlers_crud.go:449)
- Apply field policy to filter response (handlers_crud.go:456)
- Return updated document with 200 OK (handlers_crud.go:459)
Field-Level Write Permissions
The system enforcesdeny_write policies:
Immutable Field Protection
Fields marked as immutable cannot be changed:Version Snapshots
If versioning is enabled, the system saves a snapshot before updating:full- Store complete document snapshotdiff- Store only changed fieldsmetadata- Store minimal metadata only
Audit Logging
The system logs field-level changes:Error Responses
400 Bad Request
Returned when:- Invalid JSON body
- Schema validation fails
- Immutable field modified
- Pre-update hook fails
401 Unauthorized
403 Forbidden
Returned when:- User doesn’t have update permission
- Attempting to modify deny_write field
404 Not Found
Returned when document or collection doesn’t exist:500 Internal Server Error
Hooks
Pre-Update Hook
Executed before the update is applied. Can modify the document or reject the update:Post-Update Hook
Executed after successful update (best-effort, won’t fail the request):Use Cases
Status Update
Profile Update
Bulk Field Update
Related Endpoints
- Get Document - Retrieve the document
- Create Document - Create a new document
- Delete Document - Remove the document
- List Documents - Query multiple documents