Update operators for tables (HTTP)
|
For details about additional languages, see: |
Some Data API commands, like Update a row (HTTP), use update operators to modify rows in a table.
Set
The $set operator sets the value of the specified column to the specified value.
Example setting a non-map column:
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$set": {
"rating": 4.5,
"genres": ["Fiction", "Drama"]
}
}
}
}'
Example setting a map column:
If the updated map includes non-string keys, you must use an array of key-value pairs to update the map column.
Otherwise, you can use an array of key-value pairs or a normal map.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$set": {
"map_column_int_str": [
[1, "value1"],
[2, "value2"]
],
"map_column_str_str": {
"key1": "value1",
"key2": "value2"
}
}
}
}
}'
Unset
The $unset operator sets the specified column’s value to null or the equivalent empty form, such as [] or {} for map, list, and set types.
Unsetting a column produces a tombstone. Excessive tombstones can impact query performance.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$unset": {
"genres": ""
}
}
}
}'
Push
The $push operator appends a single element to a map, list, or set.
To append multiple items, use the $each operator with $push.
Example appending to a list or set column:
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$push": {
"genres": "SciFi",
"topics": {
"$each": ["robots", "AI"]
}
}
}
}
}'
Example appending to a map column:
When appending to a map, if the appended key-value pair has a non-string key, you must represent the key-value pair as an array. Otherwise, you can either use an array or a normal map.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$push": {
"map_column_int_str": [
1, "value1"
],
"map_column_str_str": {
"key1": "value1"
},
"map_column_int_str_2": {
"$each": [
[1, "value1"],
[2, "value2"]
]
},
"map_column_str_str_2": {
"$each": [
{"key1": "value1"},
["key2", "value2"]
]
}
}
}
}
}'
Each
The $each operator modifies the $push operator to append multiple items to a map, list, or set.
Example appending to a list or set column:
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$push": {
"genres": "SciFi",
"topics": {
"$each": ["robots", "AI"]
}
}
}
}
}'
Example appending to a map column:
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$push": {
"map_column_int_str": [
1, "value1"
],
"map_column_str_str": {
"key1": "value1"
},
"map_column_int_str_2": {
"$each": [
[1, "value1"],
[2, "value2"]
]
},
"map_column_str_str_2": {
"$each": [
{"key1": "value1"},
["key2", "value2"]
]
}
}
}
}
}'
Pull all
The $pullAll operator removes the specified elements from a list or set, or removes entries that match the specified keys from a map.
Example removing matches from a list or set column:
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$pullAll": {
"genres": ["SciFi", "Romance"]
}
}
}
}'
Example removing matches from map column:
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$pullAll": {
"metadata": ["language", "edition"]
}
}
}
}'
Combine operators
You can use multiple update operators in a single update.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$set": {
"rating": 4.5,
"genres": ["Fiction", "Drama"]
},
"$unset": {
"borrower": ""
}
}
}
}'
Unsupported operators
-
$renameisn’t supported. Instead, usealterTableto add and drop columns. -
$currentDateisn’t supported. -
The array operators
$addToSet,$pop, and$positionaren’t supported.