MongoDB-分页 [英] MongoDB - paging

查看:70
本文介绍了MongoDB-分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MongoDB时,是否有任何特殊的制作模式,例如分页视图? 说一个博客,其中列出了10条最新的帖子,您可以在其中浏览到较早的帖子.

When using MongoDB, are there any special patterns for making e.g. a paged view? say a blog that lists the 10 latest posts where you can navigate backwards to older posts.

或者用一个索引来解决它,例如blogpost.publishdate只是跳过并限制结果?

Or do one solve it with an index on e.g. blogpost.publishdate and just skip and limit the result?

推荐答案

当性能存在问题或有大量集合时,使用skip + limit并不是进行分页的好方法.随着页码的增加,它会越来越慢.使用跳过要求服务器将所有文档(或索引值)从0遍历到偏移(跳过)值.

Using skip+limit is not a good way to do paging when performance is an issue, or with large collections; it will get slower and slower as you increase the page number. Using skip requires the server to walk though all the documents (or index values) from 0 to the offset (skip) value.

最好使用范围查询(+限制),在其中传递最后一页的范围值.例如,如果按发布日期"进行排序,则只需传递最后一个发布日期"值作为查询条件即可获取下一页数据.

It is much better to use a range query (+ limit) where you pass in the last page's range value. For example if you are sorting by "publishdate" you would simple pass the last "publishdate" value as the criteria for the query to get the next page of data.

这篇关于MongoDB-分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆