Soru

Zorluk: KolayPerform Container and Item Operations in Azure Cosmos DB using SDK

A developer is writing a .NET application using the Azure Cosmos DB .NET SDK v3. The target container is configured with a partition key path of `/userId`. Which of the following code snippets should the developer use to retrieve a user profile item where both the item ID and the user ID are "user123"?

  1. await container.ReadItemAsync<UserProfile>("user123", new PartitionKey("user123"));Cevap
  2. B
    await container.ReadItemAsync<UserProfile>("user123");
  3. C
    await container.ReadItemAsync<UserProfile>("user123", new PartitionKey("/userId"));
  4. D
    await container.ReadItemAsync<UserProfile>("user123", new PartitionKey("USA"));

Cevap

await container.ReadItemAsync<UserProfile>("user123", new PartitionKey("user123"));
The correct code snippet uses the ReadItemAsync method passing both the item ID ("user123") and the PartitionKey object initialized with the partition key value ("user123") which matches the /userId path configuration.

Adım Adım Çözüm

1
Identify the target item ID and its partition key path.
The item ID is "user123" and the partition key path is /userId.
Point reads in Cosmos DB require both the ID and the partition key value of the document.
2
Determine the partition key value for the specific document.
Since the partition key path is /userId and the user ID is "user123", the partition key value is "user123".
The partition key value must be the value stored in the document's partition key field.
3
Select the correct SDK v3 method signature for reading an item.
container.ReadItemAsync<T>(string id, PartitionKey partitionKey)
The .NET SDK v3 requires passing both the item ID and the PartitionKey object containing the partition key value.

Anahtar Kavram

Performing point reads in Azure Cosmos DB using the .NET SDK v3 requires specifying both the unique item ID and its corresponding PartitionKey value.
Bu soruyu puanla