azure - 使用页面大小和页码在 Cosmos DB 中分页

标签 azure asp.net-core pagination azure-cosmosdb paging

我正在尝试使用 PageSizePageNumber 从 cosmosDB 返回项目。我知道我们可以在 MaxItemCount 中设置页面大小,但是我们如何在这个函数中输入页码呢?

这是我到目前为止得到的:

  public async Task<IEnumerable<T>> RunSQLQueryAsync(string queryString, int pageSize, int pageNumber)
        {
            var feedOptions = new FeedOptions { MaxItemCount = pageSize, EnableCrossPartitionQuery = true };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            var currentPageNumber = 0;
            var documentNumber = 0;
            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                foreach (T t in await query.ExecuteNextAsync())
                {
                    results.Add(t);
                    documentNumber++;
                }
                currentPageNumber++;
                return results;

            }
            return null;
        }

最佳答案

目前,分页支持仅基于延续 token

下面是一些关于此限制的有趣讨论和功能请求:

<小时/>

--- 连续 token 示例 ---

以下示例说明了一种根据所需页码、页面大小和继续标记查询文档的方法(与您的方法非常相似):

    private static async Task<KeyValuePair<string, IEnumerable<CeleryTask>>> QueryDocumentsByPage(int pageNumber, int pageSize, string continuationToken)
    {
        DocumentClient documentClient = new DocumentClient(new Uri("https://{CosmosDB/SQL Account Name}.documents.azure.com:443/"), "{CosmosDB/SQL Account Key}");

        var feedOptions = new FeedOptions {
            MaxItemCount = pageSize,
            EnableCrossPartitionQuery = true,

            // IMPORTANT: Set the continuation token (NULL for the first ever request/page)
            RequestContinuation = continuationToken 
        };

        IQueryable<CeleryTask> filter = documentClient.CreateDocumentQuery<CeleryTask>("dbs/{Database Name}/colls/{Collection Name}", feedOptions);
        IDocumentQuery<CeleryTask> query = filter.AsDocumentQuery();

        FeedResponse<CeleryTask> feedRespose = await query.ExecuteNextAsync<CeleryTask>();

        List<CeleryTask> documents = new List<CeleryTask>();
        foreach (CeleryTask t in feedRespose)
        {
            documents.Add(t);
        }

        // IMPORTANT: Ensure the continuation token is kept for the next requests
        return new KeyValuePair<string, IEnumerable<CeleryTask>>(feedRespose.ResponseContinuation, documents);
    }

现在,以下示例说明如何通过调用前面的方法来检索给定页面的文档:

    private static async Task QueryPageByPage()
    {
        // Number of documents per page
        const int PAGE_SIZE = 3;

        int currentPageNumber = 1;
        int documentNumber = 1;

        // Continuation token for subsequent queries (NULL for the very first request/page)
        string continuationToken = null;

        do
        {
            Console.WriteLine($"----- PAGE {currentPageNumber} -----");

            // Loads ALL documents for the current page
            KeyValuePair<string, IEnumerable<CeleryTask>> currentPage = await QueryDocumentsByPage(currentPageNumber, PAGE_SIZE, continuationToken);

            foreach (CeleryTask celeryTask in currentPage.Value)
            {
                Console.WriteLine($"[{documentNumber}] {celeryTask.Id}");
                documentNumber++;
            }

            // Ensure the continuation token is kept for the next page query execution
            continuationToken = currentPage.Key;
            currentPageNumber++;
        } while (continuationToken != null);

        Console.WriteLine("\n--- END: Finished Querying ALL Dcuments ---");
    }

关于azure - 使用页面大小和页码在 Cosmos DB 中分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51164399/

相关文章:

c# - 如何预热 IIS 中托管的 asp.net 核心应用程序 (a) 网站重启和 (b) 重新部署?

c# - .net core 2 with ef core migrations with docker in development

Jquery 数据表 - 所选行突出显示在第二页中不起作用

azure - 插槽算作 Azure 中的 VM 吗?

azure - 用于修改/更新 Azure VM 标记值的代码在 Azure PowerShell Runbook 中不起作用

javascript - 剑道颜色选择器值未定义

javascript - 防止多个 Ajax 请求(带 pushstate 的分页)

php - Bootstrap 分页样式不适用于代码

azure - 相当于通过 Azure Blob 的 S3 预签名 URL 上传文件

Azure - 无法解决达到允许的最大输出行数