Asynchronously loop through the mongoose collection with async.each Asynchronously loop through the mongoose collection with async.each mongoose mongoose

Asynchronously loop through the mongoose collection with async.each


Because async/await will be ES7 and is already quite popular via transpilation, this is a top result when googling for Mongoose and async.

While not answering the original question, I thought I'd leave this for future reference.

Using native Promises (note all users are processed in parallel):

const User = mongoose.model('User', new mongoose.Schema({  name: String,  age:  Number}));function processUsers() {  return mongoose.connect('mongodb://localhost')    .then(function() {      return User.find();    })    .then(function(users) {      const promises = users.map(function(user) {        return // ... some async code using `user`      });      return Promise.all(promises);    });});processUsers()  .then(function() {    console.log('Finished');  })  .catch(function(error) {    console.error(error.stack);  });

Using Mongoose's eachAsync every user is processed sequentially:

function processUsers() {  return mongoose.connect('mongodb://localhost')    .then(function() {      return User.find().cursor().eachAsync(function(user) {        return // ... some async code using `user`      });    })});

Using async/await:

async function processUsers() {  await mongoose.connect('mongodb://localhost');  await User.find().cursor().eachAsync(async function(user) {    await // ... some async code using `user`  });}


I think in our case better use waterfall, like this

async.waterfall([    function (callback) {        mongoose.connect("mongodb://localhost/****");        var db = mongoose.connection;        db.on('error', console.error.bind(console, 'connection error...'));        db.once('open', function () {            callback(null);        });    },    function(callback){        var users = mongoose.model('User', new mongoose.Schema({            name: String,            age: Number        }));        users.find(function(err, userFound) {            if (err) {                return callback(err);            }            callback(null, userFound);        });    },    function(usersData, callback){        async.each(usersData, function(userData, callback) {        }, callback);    }], function (err, result) {});

There is good explanation about difference waterfall vs series


Another option:

functionProcess = (callback) => {  userModel.find().cursor().eachAsync(user => {    return user.save().exec();        // Need promise  }).then(callback);     //Final loop}