var asynclib = require('async');
exports.apiFindNutrients = function(req, res) {
var ndb = req.params.ndb;
NutrDefModel.find().limit(30).exec(function(error, nutref) {
asynclib.mapSeries(nutref, function(ref, callback) {
NutrModel.find({ Nutr_No: ref.Nutr_No, Ndb_No: ndb }, function(error, nutrient) {
console.log(nutrient + 'nutrval: ' + nutrient.Nutr_Val);
var result = {
Ndb_No: nutrient.Ndb_No,
Nutr_No: ref.Nutr_No,
Units: ref.Units,
Tagname: ref.Tagname,
Nutr_Desc: ref.Nutr_Desc,
Nutr_Val: nutrient.Nutr_Val
};
callback(null, result);
}
});
}, function (err, result) {
if (err)
console.log('async lib ' + err);
res.send(result);
});
}
});
};
I want to join two tables with mongoose, i came up with the above solution but it takes 12 seconds for 70 items in NutrDefModel, when i do limit(30) it takes 1.5 seconds. I need to improve to under 1 seconds. I am really new to async javascript and mongoose, i barely understand how async library works in this solution, what alternetives do i have with javascript and mongoose.
I've added some indexes it improved performance, but now i have another problem. Above console.log line produces the output:
{ Ndb_No: 3198,
Nutr_No: 318,
Nutr_Val: '144',
_id: 5222665eab19d2db1800379d }nutrval: undefined
you can see nutrition object contains Nutr_Val but nutrient.Nutr_Val prints undefined. Why?