I have code that checks every object in the Mongo collection (iterates over the result of find () with no parameters) and makes changes to some of them. This seems to be unsafe: my changes are saved, but then when I continue iterating with the cursor, a subset of the changed objects (10-15%) appears a second time. I did not change the document id or anything that has an index there.
I believe that I could avoid this problem by capturing all the document identifiers ahead of time (convert the cursor to an array), but these are large collections, so I really would like to avoid this.
I noticed that the default find () result does not seem to have a specific order, so I tried to put an explicit view in the cursor, {"_id": 1}. This seems to fix the problem - now nothing appears twice no matter what I change. But I do not know if this is a good approach. As far as I can tell from the documentation, adding sorting does not make it a preliminary request for all identifiers; if so, itβs good, but then I donβt know why this will fix the problem.
Is it just a bad idea to use cursors when changing material?
I use Scala / Casbah if that matters.
Eli bishop
source share