Update documents (HTTP)
|
For details about additional languages, see: |
Finds documents in a collection using filter clauses, and then updates properties in those documents. Optionally, if no document matches the filter, inserts a new document.
This method does not support sort parameters, including vector search.
If you want to use sort parameters to find documents, use the method to find documents instead.
Then, use the method to update a single document to update the found documents by ID.
|
Ready to write code? See the examples for this method to get started. |
Result
Updates documents that match the specified parameters and returns an object that includes details about the operation, including the number of documents that were found and the number of documents that were modified.
Example response:
{
"status":{
"matchedCount":3,
"modifiedCount":2
}
}
If specified, the method inserts a new document if no document matches the filter.
Example response if a new document was inserted:
{
"status":{
"upsertedId":"1a69f13b-0331-4e85-a9f1-3b0331fe854b",
"matchedCount":0,
"modifiedCount":0
}
}
If more than 20 documents match the specified filter parameters, only the first 20 documents will be updated.
In this case, the status.matchedCount and status.modifiedCount are capped at 20, the status.moreData value is true, and the status.nextPageState value is the ID of the next page of data.
To update the next batch of 20 documents, you must send a request with the nextPageState value from your previous request.
For an example, see Update more than 20 documents.
Example response if more documents match the filter than were updated:
{
"status": {
"matchedCount": 20,
"modifiedCount": 20,
"moreData": true,
"nextPageState": "**NEXT_PAGE_STATE_ID**"
}
}
Parameters
Use the updateMany command.
Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": FILTER,
"update": UPDATE,
"options": {
"upsert": BOOLEAN
}
}
}'
| Name | Type | Summary |
|---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes. You must use For a list of available filter operators and more examples, see Filter operators for collections (HTTP). Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter. For an example, see Update documents that match a filter. |
|
|
An object that defines the updates using Data API operators. For a list of available operators and more examples, see Update operators for collections (HTTP). You must use |
|
|
Optional.
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. Whether to insert a new document if no document matches the filter criteria. For an example, see Insert a new document if no matching document exists. |
|
|
Optional.
The value of For an example, see Update more than 20 documents. |
Examples
The following examples demonstrate how to update documents in a collection.
Update documents that match a filter
You can use a filter to find documents that match specific criteria.
For example, you can find documents with an is_checked_out value of false and a number_of_pages value less than 300.
For a list of available filter operators and more examples, see Filter operators for collections (HTTP).
Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"update": { "$set": { "color": "blue" } }
}
}'
Update multiple properties
You can combine multiple operators and properties in a single call. For the full list of operators, see Update operators for collections (HTTP).
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"update": {
"$set": {
"color": "blue",
"classes": ["biology", "algebra", "swimming"]
},
"$unset": {
"phone": ""
},
"$inc": {
"age": 1
}
}
}
}'
Update nested properties
To update a nested property, use dot notation.
For example, field.subfield.subsubfield.
To update an item in a list, specify a numeric index.
For example, listProperty.3.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"update": {
"$set": {
"color": "blue",
"address.city": "Austin",
"classes.2": "biology"
}
}
}
}'
Insert a new document if no matching document exists
You can use upsert to insert a new document if no document matches the filter criteria.
In addition to any fields you explicitly set in the update parameter, the new document also includes any top-level equality conditions from the filter parameter.
Equality conditions that are nested within $and from the filter parameter are not included.
If a field is defined both in the filter and in the update, the explicitly set value takes precedence.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"update": { "$set": { "color": "blue" } },
"options": { "upsert": true }
}
}'
You can also use the setOnInsert operator to specify additional fields to set if a new document is created:
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"update": {
"$set": { "color": "blue" },
"$setOnInsert": {
"customer.name": "James"
}
},
"options": { "upsert": true }
}
}'
Update more than 20 documents
You can only update 20 documents per HTTP request.
If additional documents match your filter but were not updated, the response will include a status.moreData value of true and a non-null status.nextPageState value.
To update the next batch of 20 documents, you must send a request with the nextPageState value from your previous request. For example:
-
Send an initial request
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "updateMany": { "filter": {"$and": [ {"is_checked_out": false}, {"number_of_pages": {"$lt": 300}} ]}, "update": { "$set": { "color": "blue" } } } }' -
Get the
status.nextPageStatevalue from the response{ "status":{ "matchedCount":20, "nextPageState":"LQAAAAEBAAAAJGU5ZTY4ODViLTM2MDEtNDhhMy1hNjg4LTViMzYwMWQ4YTNmZADwf///6wA=", "modifiedCount":20, "moreData":true } } -
Use the
status.nextPageStatefrom the previous response to request the next page of results.curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "updateMany": { "filter": {"$and": [ {"is_checked_out": false}, {"number_of_pages": {"$lt": 300}} ]}, "update": { "$set": { "color": "blue" } }, "options": { "pageState": "NEXT_PAGE_STATE_FROM_PRIOR_RESPONSE" } } }' -
Once
status.nextPageStateis omitted from the response, you have updated all matching documents.{ "status":{ "matchedCount":0, "modifiedCount":0 } }
Work with . and & in field names
You must use & to escape any . or & in field names when the field is used in a filter, sort, projection, update, or indexing clause.
Dot notation, which is used to reference nested fields, should not be escaped.
For more information, see Work with . and & in field names (HTTP).
For example, in the following document, you would use escaping like this: areas.r&&d, costs.price&.usd, and costs.price&.cad.
{
"areas": {
"r&d": true,
"design": false
},
"costs": {
"price.usd": 100,
"price.cad": 90
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateMany": {
"filter": {"$and": [
{"areas.r&&d": false},
{"costs.price&.usd": {"$lt": 300}}
]},
"update": {
"$set": {
"areas.r&&d": true,
"costs.price&.usd": 310
}
}
}
}'