当在C#Azure Cosmos DB Web API中使用ReadDocumentAsync时,您如何在请求选项中对p



我需要访问存储在cosmos数据库中的项目之一,但是我没有readDocumentAsync现在需要的分区密钥的值。我正在使用过时的createCumentQuery,但是这花费了太多时间来返回对客户要求查询的回复。

该项目如下,我正在尝试将动物和相关外壳存储在数据库中,这些数据库中存储以下参数:

{
    "nextId": "1",
    "id": "2",
    "predEnclosure": "true",
    "maxCapacity": 10,
    "currentCapacity": 0,
    "animals": [],
}

我将分区密钥作为偏见,并且从理论上讲只有ID可以访问外壳。我不确定是否存在另一种方法可以从容器中检索文档。这是连接到COSMOS数据库的.NET Core Web API。

public async Task<CustomHttpResponseMessage<dynamic>> GetEnclosure(string id)
{
    try
    {
        Enclosure enclosure = await dbClient.ReadDocumentAsync<Enclosure>(
                dbHandler.GetDocumentUri(id), options);
        return new CustomHttpResponseMessage<dynamic>(HttpStatusCode.OK, enclosure);
    }
    catch (Exception e)
    {
        Console.WriteLine($"Could not get given animal! Error: {e.Message}");
        return new CustomHttpResponseMessage<dynamic>(HttpStatusCode.NotFound, "Could not find the given enclosure!");
    }
}

以上将说我需要一个分区密钥来进行该方法调用。我希望它能在不使用分区密钥的情况下从数据库中返回外壳。

这有点棘手,但您仍然可以做到这一点,

MyItem item = (dynamic)client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DbName, CollectionName, id), new RequestOptions() { PartitionKey = new PartitionKey(Undefined.Value)}).Result.Resource;

阅读以下内容,

https://www.lytzen.name/2016/12/06/find-docs-with-no-partitionkey-in-azure.html

最新更新