To update a document in Azure Cosmos DB using a REST API call, you would typically use the PUT method. Below is a sample request format for updating a document:
Request Format
Method: PUT
Request URI:
https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs/{doc-id}
Replace {databaseaccount}, {db-id}, {coll-id}, and {doc-id} with your specific values.
Headers
You will need to include the following headers in your request:
-
x-ms-documentdb-partitionkey: Required if your collection is partitioned. -
If-Match: Optional, used for optimistic concurrency control. Set this to theetagvalue of the document you are updating.
Body
The body of the request should contain the updated document in JSON format. It must include the id property, which is a unique identifier for the document. Here’s an example of the body:
{
"id": "your-document-id",
"property1": "new value",
"property2": "new value"
}
Example
Here’s a complete example of how your request might look:
PUT https://your-account.documents.azure.com/dbs/your-db/colls/your-collection/docs/your-document-id
x-ms-documentdb-partitionkey: ["your-partition-key"]
If-Match: "etag-value"
Content-Type: application/json
{
"id": "your-document-id",
"property1": "new value",
"property2": "new value"
}
Response
A successful update will return a 200 OK status code along with the updated document body in the response.
For more detailed information, you can refer to the official documentation on replacing a document in Azure Cosmos DB.
References: