'C# Cosmos DB ReadItemAsync Partition Key multiple values
I have a container that has a partition key made up of two fields. Neither of them is 'id'. In this example it is FormId and Version.
I am calling a function that uses ReadItemAsync to return a single document from the container. I'd like to include the Partition Key parameter as the data in the container is set to get very large and I'd rather not have the query run cross partition based solely on the id value. However, when I make the call I can't figure out how to structure the Partition Key value.
public async Task<dynamic> GetItemAsync(string id, string? partitionId, string containerName)
{
try
{
Container container = _client.GetContainer(_databaseName, containerName);
ItemResponse<dynamic> response;
if (partitionId == null)
response = await container.ReadItemAsync<dynamic>(id, PartitionKey.None);
else
response = await container.ReadItemAsync<dynamic>(id, new PartitionKey(partitionId));
return response.Resource;
}
catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
}
When I call with the function with the id Guid and null as the partition key, the function works fine. When I pass in a string representing the Partition Key in any of the following ways I get no data found:
formId + "." + version
formId + "/" + version
formId + "\\" + version
How should the partition key parameter be structured when it is made up of more than one field?
Edit 1: Example of the document instance:
{
"formID": "e8493c8b-6736-4360-b781-5d94e93fe545",
"version": "1",
"id": "71d7458d-b71c-467e-b5b9-2b646dbd5435",
"_rid": "QMgvAKsrCcABAAAAAAAAAA==",
"_self": "dbs/QMgvAA==/colls/QMgvAKsrCcA=/docs/QMgvAKsrCcABAAAAAAAAAA==/",
"_etag": "\"5f019a17-0000-1100-0000-6205202f0000\"",
"_attachments": "attachments/",
"_ts": 1644503087
}
The Partition Key is set as \formId.version.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
