Reputation: 21906
I have this server-side JS script:
// Run processNames on all rows in cities
var cur = db.cities.find();
cur.immortal = true;
var j = 0;
var total = cur.count();
cur.forEach(function(obj) {
processNames(obj);
if (j % 100000 === 0) {
print(j + "/" + total + " cities processed");
}
j++;
});
print("Completed load: " + j + " cities processed");
In the output, I see this:
...
4800000/3411205 cities processed
4900000/3411205 cities processed
5000000/3411205 cities processed
5100000/3411205 cities processed
5200000/3411205 cities processed
5300000/3411205 cities processed
5400000/3411205 cities processed
5500000/3411205 cities processed
How can this be happening? The cursor returns only 3411205 documents, but the forEach loop is executing over 5 million times. What's going on?
Upvotes: 1
Views: 82
Reputation: 5817
I think you modify documents in processNames()
method which causes MongoDB to relocate documents. You can use snapshot() to overcome this problem.
var cur = db.cities.find().snapshot();
Upvotes: 1