从 DocumentDB 读取单个文档
Reading single document from DocumentDB
我无法通过 DocumentDB 中的 ID 读取单个文档。我正在构建应用程序,但遇到 这样的确切问题。我不明白我错过了什么。假设我有 Users 集合并且我的分区键有 /UserId,我无法读取我总是收到 "Resource Not Found" 的文档。这很令人困惑,因为根据 Microsoft 的文档,我只能将 Uri 提供给 ReadDocumentAsync
方法,但是这样做我得到错误的分区键是必需的。我理解分区键,但我很难理解它们是如何连接到文档的。比如我的partition key是/UserId,这是一个路径,对于method ReadDocumentAsync
其实我要提供partition key的值。我很难理解那个值是什么。
我尝试使用 C# 代码创建文档并使用系统创建的 ID (GUID) 检索相同的文档,但它不起作用...这听起来很简单和基本,但有人可以指出我哪里错了吗?或者向我展示我可以使用的 C# .NET Core 代码的参考、示例集合、示例文档和示例代码。 Microsoft 的 documentation/sample 代码对我不起作用。
我的代码如下所示:
documentId = "1c230b2d-1e37-9e02-d3a0-10364a188487";
collectionId = "Users";
RequestOptions options = new RequestOptions();
options.PartitionKey = new PartitionKey("201801-M");
var document = await _serverConnection.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, documentId),options);
return document;
我的数据库是这样的:
我的分区键如下所示:
这是我在机器上使用的模拟器问题。我已经连接到实际的 CosmosDB,它工作正常...我的模拟器有问题并抛出错误,停止工作,开始工作,一旦我连接到 Azure,它就可以很好地检索示例数据。
我遇到了这个问题。我的解决方案是确保将 PartitionKey 及其值写入 json 数据对象 完全 ,因为它在集合中。例如,如果您的集合的分区键是 /state 并且您想要 "Florida" 的值,那么在您的 json 数据对象中必须在您的 json 数据中包含以下内容:"state" :"Florida" 在你的 WriteDocumentAsync 中。在我的例子中,我使用的是动态 json 数据,所以我必须将分区键和值注入到我的 json 数据对象中。如果没有正确完成,您将收到错误消息,当您尝试读取数据时,找不到具有指定 ID 的实体。
您有两种选择:1 使用匹配的分区键将数据写出到集合中,或者 2 在您的请求选项中使用 "Undefined.Value" 作为分区键。使用 Undefined.Value 将在集合中找到任何匹配的 id。
使用 ReadDocumentAsync 回读文档时,您必须将 RequestOptions.PartitionKey 设置为您要查找的所需分区键值。
希望这有帮助。
当我收到“系统中不存在具有指定 ID 的实体”时,我遇到了类似的问题,而我可以在 Azure 门户上的数据资源管理器中看到它。事实证明,我在 json 中使用整数作为分区键,并且必须在请求中以相同的方式设置它:
var documentId1 = "150:060e7249-11cd-4d5c-b666-7711d9498ab4";
var collectionId = _collectionName;
var databaseId = _databaseName;
RequestOptions options1 = new RequestOptions();
options1.PartitionKey = new PartitionKey(150);
var document = await _documentClient.ReadDocumentAsync(
UriFactory.CreateDocumentUri(databaseId, collectionId, documentId1),options1);
所以如果我写:
options1.PartitionKey = new PartitionKey("150");
请求无效。
我无法通过 DocumentDB 中的 ID 读取单个文档。我正在构建应用程序,但遇到 ReadDocumentAsync
方法,但是这样做我得到错误的分区键是必需的。我理解分区键,但我很难理解它们是如何连接到文档的。比如我的partition key是/UserId,这是一个路径,对于method ReadDocumentAsync
其实我要提供partition key的值。我很难理解那个值是什么。
我尝试使用 C# 代码创建文档并使用系统创建的 ID (GUID) 检索相同的文档,但它不起作用...这听起来很简单和基本,但有人可以指出我哪里错了吗?或者向我展示我可以使用的 C# .NET Core 代码的参考、示例集合、示例文档和示例代码。 Microsoft 的 documentation/sample 代码对我不起作用。
我的代码如下所示:
documentId = "1c230b2d-1e37-9e02-d3a0-10364a188487";
collectionId = "Users";
RequestOptions options = new RequestOptions();
options.PartitionKey = new PartitionKey("201801-M");
var document = await _serverConnection.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, documentId),options);
return document;
我的数据库是这样的:
我的分区键如下所示:
这是我在机器上使用的模拟器问题。我已经连接到实际的 CosmosDB,它工作正常...我的模拟器有问题并抛出错误,停止工作,开始工作,一旦我连接到 Azure,它就可以很好地检索示例数据。
我遇到了这个问题。我的解决方案是确保将 PartitionKey 及其值写入 json 数据对象 完全 ,因为它在集合中。例如,如果您的集合的分区键是 /state 并且您想要 "Florida" 的值,那么在您的 json 数据对象中必须在您的 json 数据中包含以下内容:"state" :"Florida" 在你的 WriteDocumentAsync 中。在我的例子中,我使用的是动态 json 数据,所以我必须将分区键和值注入到我的 json 数据对象中。如果没有正确完成,您将收到错误消息,当您尝试读取数据时,找不到具有指定 ID 的实体。 您有两种选择:1 使用匹配的分区键将数据写出到集合中,或者 2 在您的请求选项中使用 "Undefined.Value" 作为分区键。使用 Undefined.Value 将在集合中找到任何匹配的 id。 使用 ReadDocumentAsync 回读文档时,您必须将 RequestOptions.PartitionKey 设置为您要查找的所需分区键值。 希望这有帮助。
当我收到“系统中不存在具有指定 ID 的实体”时,我遇到了类似的问题,而我可以在 Azure 门户上的数据资源管理器中看到它。事实证明,我在 json 中使用整数作为分区键,并且必须在请求中以相同的方式设置它:
var documentId1 = "150:060e7249-11cd-4d5c-b666-7711d9498ab4";
var collectionId = _collectionName;
var databaseId = _databaseName;
RequestOptions options1 = new RequestOptions();
options1.PartitionKey = new PartitionKey(150);
var document = await _documentClient.ReadDocumentAsync(
UriFactory.CreateDocumentUri(databaseId, collectionId, documentId1),options1);
所以如果我写:
options1.PartitionKey = new PartitionKey("150");
请求无效。