DataGrid Pagination using Mongo dB

2019-08-14 08:34发布

问题:

I've a wpf app and mongo db as back end, if i call a query to mongo db from wpf app it giving me million of records so currently i'm using normal pagination like storing all data from db into data table and doing pagination. But if system having less memory its impossible to store that much of records, so some body says to me that mongo db provides pagination means we can call records directly from db when user clicks next or previous so any one help me to do this.

回答1:

You can use mongodb's limit() and skip() to implement paging. For example, to get the 3rd page where page size is 10, you'd use this query:

db.your_collection.find().skip(20).limit(10)


回答2:

There are two kinds of paging:

  1. Via limit and skip, just like @milan proposed.
  2. Via range query:

    db.items.find({created: {$gt: startDate, $lt: endDate})

Range queries will work faster then skip, because they are no need to move towards 'skip' item.

Look into related thread as well.

For your case if you have only next/prev buttons range paging should be good option.