node.js - Iterating over a mongodb cursor serially (waiting for callbacks before moving to next document) -
using mongoskin, can query this, return cursor:
mycollection.find({}, function(err, resultcursor) { resultcursor.each(function(err, result) { } }
however, i'd call async functions each document, , move on next item on cursor after has called (similar eachseries structure in async.js module). e.g:
mycollection.find({}, function(err, resultcursor) { resultcursor.each(function(err, result) { externalasyncfunction(result, function(err) { //externalasyncfunction completed - want move next doc }); } }
how this?
thanks
update:
i don't wan't use toarray()
large batch operation, , results might not fit in memory in 1 go.
if don't want load of results memory using toarray, can iterate using cursor following.
mycollection.find({}, function(err, resultcursor) { function processitem(err, item) { if(item === null) { return; // done! } externalasyncfunction(item, function(err) { resultcursor.nextobject(processitem); }); } resultcursor.nextobject(processitem); }
Comments
Post a Comment