Azure DocumentDB读取未找到的文档资源

我正在构建一个.Net控制台应用程序来读取DocumentDB中的信息。 控制台应用程序具有来自EventHub的数据,并在进入云时插入/更新最新数据。

我正在尝试从DocumentDB读取单个文档,我可以在请求文档之前确认文档存在。

if (DocumentDBRepository.DoesItemExist(name)) { device = await DocumentDBRepository.GetItemAsync(name); } 

我使用Microsoft的这个教程来构建用于访问DocumentDB记录的存储库,并且成功地使用了几乎所有方法。 我可以更新/删除/查询数据库,但我无法读取单数项。

首先,它抛出了一个请求PartitionKey的exception。 所以我修改了方法,将DB使用的PartitionKey添加到请求中。 一旦我添加了PartitionKey,它就会抛出另一个exception,并显示一条消息“找不到资源”

 public static async Task GetItemAsync(string id) { try { RequestOptions options = new RequestOptions(); options.PartitionKey = new PartitionKey("DeviceId"); Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), options); return (T)(dynamic)document; } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { return null; } else { throw; } } } 

PartitionKeyFromAzure

我已经修改了我的调用以使用“GetItemsAsyc”并获取IEnumerable的文档并获取列表中的第一项,但是为什么我可以使用教程中的所有其他方法但这一点仍然没有意义抛出exception说“资源未找到”。

我得到的例外情况:

 "Message: {\"Errors\":[\"Resource Not Found\"]}\r\nActivityId: e317ae66-6500-476c-b70e-c986c4cbf1d9, Request URI: /apps/e842e452-2347-4a8e-8588-2f5f8b4803ad/services/2c490552-a24d-4a9d-a786-992c07356545/partitions/0281cfdd-0c60-499f-be4a-289723a7dbf9/replicas/131336364114731886s" 

如文档中所示,您需要提供分区键的 ,而不是存储分区键的字段的名称。 因此,您需要将设备Id作为参数添加到方法中,并将其值传递给PartitionKey构造函数。

从示例:

 // Read document. Needs the partition key and the ID to be specified Document result = await client.ReadDocumentAsync( UriFactory.CreateDocumentUri("db", "coll", "XMS-001-FE24C"), new RequestOptions { PartitionKey = new PartitionKey("XMS-0001") }); 

所以对于你的代码:

 public static async Task GetItemAsync(string id, string deviceId) { try { RequestOptions options = new RequestOptions(); options.PartitionKey = new PartitionKey(deviceId); Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), options); return (T)(dynamic)document; } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { return null; } else { throw; } } } 

我今天有确切的问题。

在阅读文档db中的文档时,
当docLink的格式(dbs / dbid .. / colls / collid .. / docs / docid ..)

解:
文档客户端在以下两种情况下抛出“未找到资源”:
1.集合ID错误
2.给出partitionKey有一些问题。 也就是说,要么你提供了不正确的partitionKey,要么不需要partitionKey但是给出了一个。