'Azure Cosmos DB to fetch paged results along with all records count
The document structure is like this
[
{
"id": "1234",
"SubmittedAnswers": [
{
"PlantId": 3,
"UniqueQualityId": "3_pe55d74fc5f92b11ab3fe"
},
{
"PlantId": 4,
"UniqueQualityId": "3_pe55d74fc5sdfmsdfklms"
}
],
"CompletionTime": 36
},
{
"id": "1234",
"SubmittedAnswers": [
{
"PlantId": 3,
"UniqueQualityId": "3_pe55d74fc5f92b11ab3fe"
},
{
"PlantId": 4,
"UniqueQualityId": "3_pe55d74fc5sdfmsdfklms"
}
],
"CompletionTime": 36
}
]
In order to display the data on the grid, I had to fetch only paginated data (10 records), by using continuation token I was able get the paginated data using the following code
QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
QueryRequestOptions options = new QueryRequestOptions()
{
MaxItemCount = request.RowCount > 0 ? request.RowCount : -1,
MaxConcurrency = 10
};
FeedIterator<Submit> queryResultSetIterator =
cosmosContainer.GetItemQueryIterator<Submit>(queryDefinition,
requestOptions:options,continuationToken: request.ContinuationToken);
List<Submit> submissions = new List<Submit>();
while (queryResultSetIterator.HasMoreResults)
{
FeedResponse<Submit> currentResultSet = await queryResultSetIterator.ReadNextAsync();
continuationToken = currentResultSet.ContinuationToken;
foreach (Submit survey in currentResultSet)
{
submissions.Add(survey);
}
if (request.RowCount > 0)
break;
}
return new Response{ Answers = submissions,ContinuationToken= continuationToken };
Along with these filtered or paginated data,the total records count is also required paginate properly so for that I thought of making another call to fetch only the count of records
select value count(1) from c
Is there any better approach to fetch these paginated results and all records count together
Solution 1:[1]
Is there any better approach to fetch these paginated results and all records count together?
Afaik, the way which you are using is the recommended approach to fetch these paginated results and all records count together.
You can also do this through the Azure Portal by navigating to the Cosmos DB resource you wish to query and selecting the Data Explorer tab and using the following query: SELECT VALUE COUNT(1) FROM c
.
For more information,you can refer this link.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RajkumarMamidiChettu-MT |