Search code examples
asp.netazure-cosmosdb

How to query data with composite partition key in CosmosDb


I have a composite partition key in the cosmos db, but I do not understand how to get an item

List<(string, PartitionKey)> itemsToFind = new();
itemsToFind.Add(("testApi", new PartitionKey("[\"value1\", \"value2\"]")));
var result = await container.ReadManyItemsAsync<MyClass>(items: itemsToFind);

It throws Number of components in the partition key value does not match the definition but how do I match the number if the PartitionKey accepts only a string?


Solution

  • Have you looked at the sample in the documentation?

    // Build the full partition key path
    PartitionKey partitionKey = new PartitionKeyBuilder()
        .Add("Microsoft") //TenantId
        .Add("8411f20f-be3e-416a-a3e7-dcd5a3c1f28b") //UserId
        .Add("0000-11-0000-1111") //SessionId
        .Build();
    
    // Perform a point read
    ItemResponse<UserSession> readResponse = await container.ReadItemAsync<UserSession>(
        id,
        partitionKey
    );