Question

Difficulty: MediumPerform Container and Item Operations in Azure Cosmos DB using SDK

An enterprise hotel management application uses Azure Cosmos DB to store reservation details. The container uses the guest's ID (guestId) as the partition key. You are writing a C# helper method using the Azure Cosmos DB .NET SDK v3 that retrieves an existing booking, modifies the check-out date, and saves the changes back to the database.

Which sequence of code statements must you execute to complete these tasks?

  1. 1CosmosClient client = new CosmosClient(connectionString);
  2. 2Database database = client.GetDatabase(databaseId);
  3. 3Container container = database.GetContainer(containerId);
  4. 4ItemResponse<Booking> response = await container.ReadItemAsync<Booking>(bookingId, new PartitionKey(guestId));
  5. 5response.Resource.CheckOutDate = newCheckOutDate;
  6. 6await container.ReplaceItemAsync<Booking>(response.Resource, bookingId, new PartitionKey(guestId));

Answer

Initialize the CosmosClient, obtain references to the database and container, execute ReadItemAsync to fetch the booking, modify the checkout date property, and call ReplaceItemAsync with the updated object and partition key.
To update an existing item in Azure Cosmos DB using the .NET SDK v3, you must first initialize a CosmosClient and drill down to the Container reference. From there, you perform a point read using ReadItemAsync to fetch the item, which requires the item ID and the PartitionKey. After modifying the deserialized object exposed via the Resource property of the response, you call ReplaceItemAsync, again specifying the updated object, the item ID, and the PartitionKey to save the changes.

Step-by-Step Solution

1
Initialize the CosmosClient instance using the connection string.
A CosmosClient object is created to manage connections to the Azure Cosmos DB account.
The client is the entry point for all Cosmos DB SDK operations.
2
Get a Database reference using the client.
A Database object representing the target database.
You must drill down through the hierarchy to get a container reference.
3
Get a Container reference using the database.
A Container object representing the target container.
Item operations like ReadItemAsync and ReplaceItemAsync are invoked on the Container object.
4
Retrieve the booking item by calling ReadItemAsync.
An ItemResponse containing the Booking object in its Resource property.
You must read the current state of the document from the server before modifying it.
5
Modify the guest's checkout date property on the deserialized object.
The local Booking object has its checkout date updated.
The modification must be applied locally before being sent to the database.
6
Persist the modified object using ReplaceItemAsync.
The item is updated in the Cosmos DB container.
This updates the existing document on the server using its unique ID and partition key.

Key Concept

Performing point read and replacement operations on items using the Cosmos DB .NET SDK v3.
Rate this question