Range based paging mongodb

微笑、不失礼 提交于 2019-11-27 11:21:28

问题


on the mongodb docs it says: (source)

Unfortunately skip can be (very) costly and requires the server to walk from the beginning of the collection, or index, to get to the offset/skip position before it can start returning the page of data (limit). As the page number increases skip will become slower and more cpu intensive, and possibly IO bound, with larger collections. Range based paging provides better use of indexes but does not allow you to easily jump to a specific page.

What is range based paging and where is the documentation for it?


回答1:


The basic idea is to write the paging into the query predicate pattern.

For example if you list forum posts by date and you want to show the next page then use the date of the last post on the current page as a predicate. MongoDB can use the index built on the date field.

//older posts
db.forum_posts.find({date: {$lt: ..last_post_date..} }).sort({date: -1}).limit(20);

Of course this gets a little more complicated if the field you are using for sorting is not unique.



来源:https://stackoverflow.com/questions/6806063/range-based-paging-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!