【发布时间】:2022-09-23 14:10:45
【问题描述】:
当尝试从 CosmosDb 读取时,我可以通过以下方式选择文档:
- 身份证查询
- Id + PartitionKey 查询
但是当我只有 PartitionKey 时,如何从 CosmosDb 中选择数据?
using Microsoft.Azure.Cosmos;
public class CosmosDbService : ICosmosDbService
{
private Container _container;
public CosmosDbService(
CosmosClient cosmosDbClient,
string databaseName,
string containerName)
{
_container = cosmosDbClient.GetContainer(databaseName, containerName);
}
public async Task<Error> GetItemAsync(string partitionKey)
{
// selection only via partitionKey - does not work
var response = await _container.ReadItemAsync<Error>(partitionKey, new PartitionKey(partitionKey));
return response.Resource;
// below one works as i am passing the Id (internally generated by CosmosDB)
var id = \"2e4e5727-86ff-4c67-84a6-184b4716d744\";
var response = await _container.ReadItemAsync<Error>(id, new PartitionKey(partitionKey));
return response.Resource;
}
}
问题: CosmosDB 客户端中是否还有其他方法可以仅使用 PartitionKey 返回文档而无需我不知道的 Id?
-
您是将
/customerId作为输入传递给GetItemAsync方法还是客户ID 的实际值。您需要传递实际值而不是分区键属性名称。 -
是的,我正在传递 CustomerId 值
-
因此,当您将
partitionKey传递为2e4e5727-86ff-4c67-84a6-184b4716d744时,您不会得到任何数据。那是对的吗? -
我正在尝试使用“CustomerId”获取记录,但它不会返回数据,除非我同时传递“Id-->csomosDB 内部生成”和作为分区键的 CustomerId
-
要读取单个项目,您需要同时传递文档 ID 和分区键值。
标签: azure-cosmosdb