How to retrieve single document from azure cosmosdb collection(如何从azure cosmosdb集合中检索单个文档)
本文介绍了如何从azure cosmosdb集合中检索单个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始使用cosmosdb,在查询文档时遇到了问题。 我的文档如下所示:
{
"id": "2",
"url": "cars/toyota-auris",
"name": "Toyota Auris",
"description": "Auris desc",
"address": {
"city": "Berlin"
},
"_rid": "--4zAKfcoCgCAAAAAAAAAA==",
"_self": "dbs/--2zAA==/colls/--4zAKfcoCg=/docs/--4zAKfcoCgCAAAAAAAAAA==/",
"_etag": ""23010db4-0000-0d00-0000-5f1b5f4a0000"",
"_attachments": "attachments/",
"_ts": 1595629383
}
Partition key: address/city
我正在尝试使用cosmosd.Net SDK 3检索文档。
int id = "2";
ItemResponse<Car> response = await _container.ReadItemAsync<Car>(id, new PartitionKey(id));
return response.Resource;
此查询以异常结束:
Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId: c2c2bbff-5451-44af-a397-ca67088cce02; Reason: ({
"Errors": [
"Resource Not Found"
]
});'
有趣的是,我可以使用
查询所有文档_container.GetItemQueryIterator<Car>(new QueryDefinition("SELECT * FROM Cars"));
查询单个文档我做错了什么?
推荐答案
看起来您正在使用id
作为分区键进行查询,但您定义的分区键是address/city
。因为ReadItemAsync只在指定的分区中查找,所以它不会找到您的项目。如果要执行点读取,则需要选择预先知道的分区键。
这篇关于如何从azure cosmosdb集合中检索单个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何从azure cosmosdb集合中检索单个文档


猜你喜欢
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01