当您不知道partitionkey时使用DocumentClient.ReadDocumentAsync

时间:2020-08-20 14:02:42

标签: c# azure azure-cosmosdb asp.net-core-webapi .net-core-3.1

我遇到一种情况,我需要检索特定文档,但是我不知道它是PartitionKey。

方法如下:

    public async Task<T> GetItemAsyncNoGroupId(string id)
    {
        try
        {
            Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id),
                new RequestOptions() { PartitionKey = new PartitionKey(Undefined.Value) });

            return (T)(dynamic)document;
        }
        catch (DocumentClientException e)
        {
            if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return null;
            }
            else
            {
                throw;
            }
        }
    }

我将Undefined.Value传递给PartitionKey对象,但这没有用。我也没有将任何PartionKey对象传递到ReadDocumentAsync方法中,这是行不通的。我总是回传“无内容”。关于如何使它起作用的任何建议?

1 个答案:

答案 0 :(得分:2)

您需要使用跨分区查询。阅读here