Is there a way I can process the request to process next NumberOfRecord only, and return TotalOfRecords when PageIndex is requested?
Hi Nam,
It’s been a while since you posted the question, have you found a solution ?
Based on the context of question I assumed you are using ASP.Net GridView to do pagination. There are a number of ways to do pagination in MongoDB and it depends on your use case and dataset and performance requirements.
If you have a large dataset, try to use the custom pagination of GridView by setting AllowCustomPaging="true"
. You would need to write your own methods to step through the pages. For more information on custom paging of GridView see :
In terms of querying MongoDB, you could try using the combination of skip and ranged pagination of _id
and only show a handful of pages. For example:
First ... 21 22 23 24 25 ... Last
This way you don’t have to list all of the pages, and reducing the cost of skipping too many records. An example snippet of C# query that combines range and skip would be:
var pagesize = 10;
var filterBuilder = Builders<BsonDocument>.Filter;
var filter = filterBuilder.Lte("_id", currentObjectId);
var sort = Builders<BsonDocument>.Sort.Descending("_id");
var result = collection.Find(filter)
.Skip( pagenum * pagesize)
.Limit(pagesize)
.Sort(sort).ToList();
See also the answer of StackOverflow: MongoDB Ranged Pagination.
Kind regards,
Wan.