Paging collection with C# Mongo Driver

2,665 views
Skip to first unread message

Nam Đỗ Đức

unread,
Mar 15, 2016, 5:46:44 AM3/15/16
to mongodb-user
Hi all,

I can not find document about over a large number of records with C# Mongo Driver.

Is there a way I can process the request to process next NumberOfRecord only, and return TotalOfRecords when PageIndex is requested?

Please help on this.

Wan Bachtiar

unread,
Apr 21, 2016, 2:00:34 AM4/21/16
to mongodb-user

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.

Reply all
Reply to author
Forward
0 new messages