MongoDB Java MongoCursor 快照的等效驱动程序

MongoDB Java Driver Equivalent of Snapshot for MongoCursor

将我的 MongoDB Java 驱动程序从版本 2.14 升级到 3.2 后,我不再使用 DBCursor to MongoCursor

以前,我使用 snapshot() 来防止在遍历包含数千个文档的大型数据库时重复。但是,我似乎找不到 MongoCursor 的等效方法。这会导致令人不安的重复,例如4493 个文档的 5571 个循环。这相当于多了 24% 的迭代!天哪!

所以,我的问题是,MongoCursor 是否有一种简单的方法或等效方法可以防止这种情况发生?如果不是,我应该切换回使用 DBCursor 吗?它看起来在 3.2 版本中仍然受支持。

请多多指教!谢谢!

通过检查探查器日志进行了一些操作后,我实际上得到了确认:

MongoCursor<Document> cursor = collection.find().modifiers(
    new Document("$snapshot", true)
).iterator();

所以你需要调用.modifiers() while still on a FindIterable with $snapshot as true. This is consistent over the wire with the .snaphot()游标修饰符。

两者都像这样在分析器中记录:

   "query" : {
            "find" : "sample",
            "filter" : {

            },
            "snapshot" : true
    },

显示正确放置的修饰符。