Update record
The update record API endpoint allows you to update an existing record in a schema. To update a record, you need to send a PUT request to the /api/content/{schema}/{id} endpoint with the update data in the request body.
Supported operations
The update record API supports a variety of update operations, includes:
- Update a single field.
- Updating
o2oando2mrelation fields. - Adding and removing items from
m2mrelation fields. - Clearing the value of a field.
- Performing expressions-based updates.
Update data
The update data is a JSON object that contains the field data to update or the update operations to perform.
Simple field update
To update a single field, you can provide the field name and the new value in the update data.
Example
{
"name": "Updated Post",
"priority": 2,
"category": { "id": "550e8400-e29b-41d4-a716-446655440001" },
"tags": [{ "id": "550e8400-e29b-41d4-a716-446655440002" }, { "id": "550e8400-e29b-41d4-a716-446655440003" }]
}A simple field update contains the schema field names as keys and the new values as values. The new values can be one of the following:
- A simple value to update the field. For example, the above update data updates the
namefield toUpdated Postand thepriorityfield to2. - An object containing the new value for the relation field. For example, the above updates the
categoryfield to the category with the specified UUID and thetagsfield to the tags with the specified UUIDs.
Operation-based update
To perform an operation-based update, you can use the operation as the key and the operation data as the value in the update data.
Supported operations:
$set$clear$add$expr
Example
{
"$set": {
"name": "Updated Post",
"priority": 2,
"category": { "id": "550e8400-e29b-41d4-a716-446655440001" }
},
"$clear": {
"tags": true
},
"$add": {
"tags": [{ "id": "550e8400-e29b-41d4-a716-446655440002" }, { "id": "550e8400-e29b-41d4-a716-446655440003" }]
},
"$expr": {
"views": "views + 1",
"description": "CONCAT(description, ' Updated')"
}
}$set operation
The $set operation allows you to update the field with the new value.
Example
{
"$set": {
"name": "Updated Post",
"priority": 2,
"category": { "id": "550e8400-e29b-41d4-a716-446655440001" },
"tags": [{ "id": "550e8400-e29b-41d4-a716-446655440002" }, { "id": "550e8400-e29b-41d4-a716-446655440003" }]
}
}The $set property accepts an object with:
- The field names as keys: The field can be a simple field or a relation field.
- The new values as values: The new values can be simple values or objects for relation fields.
- A simple value:
- Set the
namefield toUpdated Post. - Set the
priorityfield to2.
- Set the
- A single relation field (single entity):
- Set the
categoryfield to{ "id": "550e8400-e29b-41d4-a716-446655440001" }.
- Set the
- Multiple relation fields (array of entity):
- Set the
tagsfield to[{ "id": "550e8400-e29b-41d4-a716-446655440002" }, { "id": "550e8400-e29b-41d4-a716-446655440003" }].
- Set the
- A simple value:
$clear operation
The $clear operation allows you to clear the value of a field, including relation fields.
Example
{
"$clear": {
"description": true,
"category": true,
"tags": [{ "id": "550e8400-e29b-41d4-a716-446655440002" }]
}
}The $clear property accepts an object with the field names as keys and the values as one of the following:
true: Clear the field value.- Clear a simple field: Set the
descriptionfield tonull. - Clear all the relation fields: Set the
categoryto empty.
- Clear a simple field: Set the
- An array of objects: Clear the specific items from the
o2morm2mrelation field.- Remove the specified UUIDs from the
tagsrelation field.
- Remove the specified UUIDs from the
$add operation
The $add operation allows you to increment a numeric field or add new items to the o2m or m2m relation fields.
Example
{
"$add": {
"views": 1,
"tags": [{ "id": "550e8400-e29b-41d4-a716-446655440002" }, { "id": "550e8400-e29b-41d4-a716-446655440003" }]
}
}The $add property accepts an object with the field names as keys and the values as one of the following:
- A numeric value: Increment the
viewsfield by1. - An array of objects: Add new items to the
o2morm2mrelation fields.
$expr operation
The $expr operation allows you to perform expressions-based updates on the fields.
Example
{
"$expr": {
"views": "views + 1",
"description": "CONCAT(description, ' Updated')"
}
}The $expr property accepts an object with the field names as keys and the values as the expressions to update the field.
The values of the $expr property must be a valid SQL expression that can be evaluated by the database.
Example
To update a user record with a new data:
PUT /api/content/post/550e8400-e29b-41d4-a716-446655440000 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: en;q=0.9,ja;q=0.8
Authorization: Bearer <jwt token>
Cache-Control: max-age=0
Connection: keep-alive
Content-Type: application/json;charset=utf-8
Cookie: token=<jwt token>
Host: localhost:8000
Referer: http://localhost:8000/dash/
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36{
"name": "John Doe",
"age": 30,
"room": { "id": "550e8400-e29b-41d4-a716-446655440010" },
"pets": [ { "id": "550e8400-e29b-41d4-a716-446655440011" }, { "id": "550e8400-e29b-41d4-a716-446655440012" } ],
"groups": [ { "id": "550e8400-e29b-41d4-a716-446655440013" }, { "id": "550e8400-e29b-41d4-a716-446655440014" } ],
"$set": {
"bio": "Hello World",
"address": "123 Main St",
"department": { "id": "550e8400-e29b-41d4-a716-446655440020" },
"roles": [ { "id": "550e8400-e29b-41d4-a716-446655440021" }, { "id": "550e8400-e29b-41d4-a716-446655440022" } ],
"projects": [ { "id": "550e8400-e29b-41d4-a716-446655440023" }, { "id": "550e8400-e29b-41d4-a716-446655440024" } ]
},
"$clear": {
"bio": true,
"address": true,
"room": true,
"roles": true,
"projects": true,
"pets": [ { "id": "550e8400-e29b-41d4-a716-446655440011" }, { "id": "550e8400-e29b-41d4-a716-446655440012" } ],
"groups": [ { "id": "550e8400-e29b-41d4-a716-446655440013" }, { "id": "550e8400-e29b-41d4-a716-446655440014" } ]
},
"$add": {
"pets": [ { "id": "550e8400-e29b-41d4-a716-446655440011" }, { "id": "550e8400-e29b-41d4-a716-446655440012" } ],
"groups": [ { "id": "550e8400-e29b-41d4-a716-446655440013" }, { "id": "550e8400-e29b-41d4-a716-446655440014" } ],
"age": 1,
"salary": 1000
},
"$expr": {
"bio": "LOWER(`bio`)",
"address": "CONCAT(`address`, ' ', `city`, ' ', `state`, ' ', `zip`)"
}
}