MongoDB分页的范围查询 [英] Range query for MongoDB pagination

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

问题描述

我想在MongoDB之上实现分页。对我的范围查询,我想到使用ObjectIDs:

  db.tweets.find({_id:{$ lt:maxID} },{limit:50})

但是,根据文档,ObjectID的结构意味着ObjectId值不表示严格的插入顺序:


ObjectId值的顺序和生成时间之间的关系在一秒钟内不严格。 如果单个系统上的多个系统或多个进程或线程在一秒钟内生成值, 客户端之间的时钟偏移也可能导致对值的非严格排序,因为客户端驱动程序生成ObjectId值,而不是mongod进程。


然后我想到用一个时间戳查询:

  db。 tweets.find({created:{$ lt:maxDate}},{limit:50})

但是,不能保证日期是唯一的 - 很可能在同一秒钟内创建两个文档。



是否有任何种类的范围查询可以为我提供更多的稳定性?

解决方案

使用ObjectId()是完全正常的,虽然你的语法分页是错误的。您需要:

  db.tweets.find()。limit(50).sort({_ id: -  1} ); 

这说明你需要按 _id 值,并且您想要最近的50.您的问题是,当当前结果集正在改变时,分页是棘手的 - 因此,而不是使用跳过下一页,您要记下最小的 _id 在结果集中(第50个最近的 _id 值,然后获取下一页:

  db.tweets.find({_id:{$ lt:< 50th_id>}}).limit(50).sort _id: -  1}); 

这将给你下一个最近的tweets,



完全没有必要担心是否 _id value是严格对应于插入顺序 - 它将是99.999%足够接近,没有人真正关心sub-second级别的推文先来 - 你可能甚至注意到Twitter经常显示tweets不按顺序,这不是那么关键。 / p>

如果是重要的,那么您将必须使用相同的技术,但使用tweet date,其中该日期必须是时间戳,而不仅仅是一个日期。


I want to implement pagination on top of a MongoDB. For my range query, I thought about using ObjectIDs:

db.tweets.find({ _id: { $lt: maxID } }, { limit: 50 })

However, according to the docs, the structure of the ObjectID means that "ObjectId values do not represent a strict insertion order":

The relationship between the order of ObjectId values and generation time is not strict within a single second. If multiple systems, or multiple processes or threads on a single system generate values, within a single second; ObjectId values do not represent a strict insertion order. Clock skew between clients can also result in non-strict ordering even for values, because client drivers generate ObjectId values, not the mongod process.

I then thought about querying with a timestamp:

db.tweets.find({ created: { $lt: maxDate } }, { limit: 50 })

However, there is no guarantee the date will be unique — it's quite likely that two documents could be created within the same second. This means documents could be missed when paging.

Is there any sort of ranged query that would provide me with more stability?

解决方案

It is perfectly fine to use ObjectId() though your syntax for pagination is wrong. You want:

 db.tweets.find().limit(50).sort({"_id":-1});

This says you want tweets sorted by _id value in descending order and you want the most recent 50. Your problem is the fact that pagination is tricky when the current result set is changing - so rather than using skip for the next page, you want to make note of the smallest _id in the result set (the 50th most recent _id value and then get the next page with:

 db.tweets.find( {_id : { "$lt" : <50th _id> } } ).limit(50).sort({"_id":-1});

This will give you the next "most recent" tweets, without new incoming tweets messing up your pagination back through time.

There is absolutely no need to worry about whether _id value is strictly corresponding to insertion order - it will be 99.999% close enough, and no one actually cares on the sub-second level which tweet came first - you might even notice Twitter frequently displays tweets out of order, it's just not that critical.

If it is critical, then you would have to use the same technique but with "tweet date" where that date would have to be a timestamp, rather than just a date.

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

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