基于范围的分页 mongodb [英] Range based paging mongodb

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

问题描述

在 mongodb 文档上它说:(来源)

on the mongodb docs it says: (source)

不幸的是,跳过可能(非常)昂贵并且需要服务器从集合或索引的开头开始,到达在开始返回数据页之前偏移/跳过位置(限制).随着页码的增加,skip 会变得越来越慢cpu 密集型,可能是 IO 绑定,具有更大的集合.范围基于分页可以更好地利用索引,但不允许您轻松跳转到特定页面.

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?

推荐答案

基本思路是将分页写入查询谓词模式.

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

例如,如果您按日期列出论坛帖子并希望显示下一页,则使用当前页面上最后一个帖子的日期作为谓词.MongoDB 可以使用建立在日期字段上的索引.

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.

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

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