sup*_*nja 12 paging pagination azure asp.net-core azure-cosmosdb
我正在尝试使用PageSize和返回cosmosDB中的项目PageNumber.我知道我们可以设置页面大小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;
}
Run Code Online (Sandbox Code Playgroud)
Eva*_*ula 17
目前,分页支持仅基于延续令牌.
在下面找到有关此限制的一些有趣的讨论和功能请求:
---继续令牌示例---
以下示例说明了一种方法(与您的方法非常相似),该方法根据所需的页码,页面大小和延续令牌查询文档:
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);
}
Run Code Online (Sandbox Code Playgroud)
现在,以下示例说明了如何通过调用以前的方法来检索给定页面的文档:
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 ---");
}
Run Code Online (Sandbox Code Playgroud)
Poo*_*eld 11
现在可以通过新的OFFSET LIMIT子句在Cosmos DB中使用“跳过并获取”:https : //docs.microsoft.com/zh-cn/azure/cosmos-db/sql-query-offset-limit
| 归档时间: |
|
| 查看次数: |
9273 次 |
| 最近记录: |