node.jsmongodbexpressmongoose

how can run mongoose query in forEach loop


How can I run mongoose query in forEach loop in nodejs and get inner join result need of both collections?

like below details

userSchema.find({}, function(err, users) {
    if (err) throw err;
    users.forEach(function(u,i){
        var users = [];
        jobSchema.find({u_sno:s.u.sno}, function(err, j) {
            if (err) throw err;
            if (!u) {
                res.end(JSON.stringify({
                    status: 'failed:Auction not found.',
                    error_code: '404'
                }));
                console.log("User not found.");
                return 
            }
            users.push(j);
        })
    })
    res.send(JSON.stringify({status:"success",message:"successfully done",data:{jobs:j,users:u}}));
})

Solution

  • You can use this:

    db.collection.find(query).forEach(function(err, doc) {
       // ...
    });