This has me stumped.
I have a standalone (command-line executed) node script, whose purpose is to iterate through all the documents in a large collection (several hundred thousand of them), and for each document, perform a few calculations, run a little additional JS code, and then update the document with some new values.
Per the documentation for cursor.each()
, once I've got my cursor from collection.find()
, the .each(cb)
method should execute cb(item)
on each item in the entire collection.
Example code:
myDb.collection('bigcollection').find().each(function(err, doc) {
if (err) {
console.log("Error: " + err);
} else {
if (doc != null) {
process.stdout.write(".");
} else {
process.stdout.write("X");
}
}
});
What I'd expect this to do is print out several hundred thousand .
's and then print an X
at the end, as cursor.each()
is supposed to "Iterate over all the documents for this cursor," and per the example code, "If the item is null then the cursor is exhausted/empty and closed."
But what it actually does is print out precisely 101 .
's, without an X
at the end.
If I adjust the batch size (.find().batchSize(10).each(...
), it goes through exactly that number of documents before bailing.
So, why is it only processing the first batch? Am I somehow misreading the documentation for .each()? Does it have to do with the fact that this is a command-line script, and somehow the whole script is exiting before the second batch of results comes back, or something? If so, how do I make sure it actually processes all the results?
As a side node, I've tried using .stream() and .forEach(), and in both of those cases as well, it ditches after the first batch.
UPDATE: Well, this is interesting. Just tried connecting to my production server instead of my mongo instance on localhost, and voila, it runs through the entire collection like it should. The server is running mongodb 3.0.6, my local instance is 3.2.3. My version of the node mongodb driver is 2.0.43.
null
. The last item is the last item... period. So it is a valid document and it will print.
, notx
. Maybe you need to count the collection first and use a counter to printx
after the last one. Makes sense? – BreedermyDb
before the iteration completes? – Thermionics