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

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

问题描述

在mongodb文档上它说: (源)

on the mongodb docs it says: (source)

不幸的是,跳过可能(非常)昂贵,并且需要服务器执行 从集合或索引的开头走到 偏移/跳过位置,然后才能开始返回数据页 (限制).随着页码的增加,跳过会变得越来越慢 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天全站免登陆