MongoDB - 分页 [英] MongoDB - paging

查看:27
本文介绍了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天全站免登陆