Async: Combining two mongodb collection using Async.forEach
The simplest and most efficient way of doing this simple task is by using the aggregation framework where you can leverage mongo's native operators like $match
to filter the document stream to allow only matching documents to pass unmodified into the next pipeline stage and $lookup
to perform a left outer join to the payment collection in the same database to filter in documents from the "joined" collection for processing:
var data = req.body;OrderModel.aggregate([ { "$match": { "table_no": data.table_no } }, { "$lookup": { "from": "payments", "localField": "_id", "foreignField": "order_id", "as": "payments" } }]).exec(function (err, result){ if (err){ res.status('500').send(err); } res.send(result)});
However, as it stands your code is failing here
PaymentModel.find({ order_id: vorders.id }, function(err, payments){
since vorders
object does not have any id
key but _id
, so that should be
PaymentModel.find({ "order_id": vorders._id }, function(err, payments){