简体   繁体   中英

MongoDB Java Driver Equivalent of Snapshot for MongoCursor

After upgrading my MongoDB Java driver from version 2.14 to 3.2, I changed from using DBCursor to MongoCursor .

Previously, I was using snapshot() to prevent repetition when iterating through my large database of thousands of documents. However, I can't seem to find equivalent method for MongoCursor. This is causing troubling repetitions, eg 5571 loops for 4493 documents. That's like 24% more iterations! OMG!

So, my question is, is there a simple way or an equivalent method for MongoCursor that can prevent this from happening? If not, should I switch back to using DBCursor? It looks to still be supported in version 3.2.

Please kindly advise! Thank you!

After banging a few things through an checking the profiler logs I actually got a confirmation on this:

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

So you need to call the .modifiers() while still on a FindIterable with $snapshot as true . This is consistent over the wire with the .snaphot() cursor modifier.

Both record in the profiler like this:

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

            },
            "snapshot" : true
    },

Showing the correct modifier placed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM