CosmosDB 分页返回值

问题描述 投票:0回答:2

我正在尝试返回来自 CosmosDB 的请求的分页结果。我从here看到了这个例子,但我不确定如何处理

response
变量。

// Fetch query results 10 at a time.
var queryable = client.CreateDocumentQuery<Book>(collectionLink, new FeedOptions { MaxItemCount = 10 });
while (queryable.HasResults)
{
    FeedResponse<Book> response = await queryable.ExecuteNext<Book>();
}

我应该直接退货吗?或者我是否需要对

response
变量做进一步的事情?我尝试直接返回响应变量,但它不起作用。这是我的代码:

 public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
        {

            var feedOptions = new FeedOptions { MaxItemCount = 3 };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            var response = new FeedResponse<T>();
            while (query.HasMoreResults)
            {
                response = await query.ExecuteNextAsync<T>();
            }
            return response;
        }

更新: 阅读@Evandro Paula 的答案后,我按照 URL 并将我的实现更改为以下。但它仍然给我 500 状态代码:

 public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
        {
            var feedOptions = new FeedOptions { MaxItemCount = 1 };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                foreach (T t in await query.ExecuteNextAsync())
                {
                    results.Add(t);
                }
            }
            return results;
        }

这是异常消息:

需要跨分区查询,但已禁用。请设定 x-ms-documentdb-query-enablecrosspartition 为 true,指定 x-ms-documentdb-partitionkey,或修改您的查询以避免这种情况 例外。,Windows/10.0.17134 documentdb-netcore-sdk/1.9.1

更新2: 我将

EnableCrossPartitionQuery
添加为 true,并且我能够从 CosmosDB 获得响应。但我无法获取我定义的 1 项。相反,我得到了 11 件物品。

azure rest asp.net-core pagination azure-cosmosdb
2个回答
3
投票

下面是有关如何使用 CosmosDB/SQL 分页查询的简单示例:

    private static async Task Query()
    {
        Uri uri = new Uri("https://{CosmosDB/SQL Account Name}.documents.azure.com:443/");
        DocumentClient documentClient = new DocumentClient(uri, "{CosmosDB/SQL Account Key}");

        int currentPageNumber = 1;
        int documentNumber = 1;

        IDocumentQuery<Book> query = documentClient.CreateDocumentQuery<Book>("dbs/{CosmoDB/SQL Database Name}/colls/{CosmoDB/SQL Collection Name}", new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery();

        while (query.HasMoreResults)
        {
            Console.WriteLine($"----- PAGE {currentPageNumber} -----");

            foreach (Book book in await query.ExecuteNextAsync())
            {
                Console.WriteLine($"[{documentNumber}] {book.Id}");
                documentNumber++;
            }

            currentPageNumber++;
        }
    }

根据问题中描述的异常需要跨分区查询但已禁用,请按如下方式更新源选项:

var feedOptions = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true};

https://github.com/Azure/azure-documentdb-dotnet/blob/d17c0ca5be739a359d105cf4112443f65ca2cb72/samples/code-samples/Queries/Program.cs#L554-L576.

查找更全面的示例。

1
投票

您没有为您的特定项目指定任何条件...因此您将获得所有结果..尝试为您正在查找的项目(id、名称等)指定条件。请记住,跨分区查询一次会消耗更多的 RU,您可以重新审视数据模型的架构。理想情况下,始终在同一分区中进行查询

© www.soinside.com 2019 - 2024. All rights reserved.