本文介绍了MongoDB 上限集合和单调递增索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在 MongoDB 上限集合中,我是否可以保证 _id 字段随着插入顺序单调递增?如果没有,如何查询在给定文档之后插入的所有文档?不需要任何索引,因为我应该能够利用它们的自然顺序.

In MongoDB capped collections, do I have the guarantee that the _id field is monotically increasing with the order of inserts? If not, how can I query for all the documents that were inserted after a given document? There shouldn't be any need for an index as I should be able to leverage their natural order.

推荐答案

ObjectId 的一部分是时间戳.所以,如果 ObjectIds 总是在同一台机器上生成(或者时间是一致的),那么你可以保证单调递增的值.不过有一个警告:时间戳以秒为单位,而不是毫秒.因此,不能保证在相同的二阶值内.

Part of ObjectId is a timestamp. So, if ObjectIds are always generated on the same machine (or otherwise time is consistent), then you are guaranteed monotonically increasing values. One caveat, though: timestamp is in seconds, not milliseconds. So, within the same second order of values is not guaranteed.

上限集合支持特殊排序选项:$natural.这意味着文档将按插入顺序返回.

Capped collections support a special sorting option: $natural. That means that documents will be returned in insertion order.

您可以将其与 tailable cursors 结合使用,以连续获取新插入的文档,而无需使用任何索引(如果这就是您所追求的).

You can combine this with tailable cursors to continually fetch newly inserted documents without using any indexes (if that's what you're after).

这篇关于MongoDB 上限集合和单调递增索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:20