Mongo DB:获取在已知的最后一个文件之后插入的所有文件 [英] Mongo DB: Get all documents inserted after the last known one

查看:48
本文介绍了Mongo DB:获取在已知的最后一个文件之后插入的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> db.events.find()
{ "_id" : ObjectId("50911c3e09913b2c643f1215"), "context" : "jvc8irfjc9cdnf93", "key" : "value" }
{ "_id" : ObjectId("50911c4709913b2c643f1216"), "context" : "jvc8irfjc9cdnf93", "key" : "new value" }
{ "_id" : ObjectId("50911c4b09913b2c643f1217"), "context" : "jvc8irfjc9cdnf93", "key" : "newer value" }
{ "_id" : ObjectId("50911c4f09913b2c643f1218"), "context" : "jvc8irfjc9cdnf93", "key" : "newest value" }
{ "_id" : ObjectId("50911c6309913b2c643f1219"), "context" : "0djd8vcndkfnjhv3", "key" : "value" }

是Mongo DB服务器上的事件列表.客户跟踪他知道的最后一个事件,例如第二个事件.

如何获取此已知事件之后已插入的所有事件?在这种情况下,50911c4b09913b2c643f121750911c4f09913b2c643f121850911c6309913b2c643f1219.

解决方案

之所以在这里看到对象ID的升序是因为该规范说: time|machine|pid|inc是用于创建ObjectId的格式.

请注意,ObjectId中已经有一个时间分量,但是以秒为单位.
Mongo中的Date类型表示从纪元开始的毫秒数,这将使您更加精确地确定插入时间.

我认为最好的方法是使用

is a list of events on a Mongo DB server. The client keeps track of the last event he knows about, for example 50911c4709913b2c643f1216, the second one.

How to I get all events that have been inserted after this known one? In this case 50911c4b09913b2c643f1217, 50911c4f09913b2c643f1218 and 50911c6309913b2c643f1219.

解决方案

The reason why you are seeing the object ids in increasing order here is because the spec says that time|machine|pid|inc is the format for creating the ObjectId.

Notice that there is already a time component in the ObjectId , but that is in seconds.
The Date type in Mongo is the representation of the number of milliseconds from epoch, which will give you some more precision for figuring out time of insertion.

I think the best way is to use a counter in the form of Sequence numbers if you need absolute precision beyond milli seconds, but if you think that your app is not that write intensive just use the Date type in the form of an additional key called timestamp.

这篇关于Mongo DB:获取在已知的最后一个文件之后插入的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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