我使用Node JS,Express和MongoDB来创建API,对于这个API,我需要从两个集合中获取数据并发送组合响应,但我无法在最终响应中获取所需数据
router.get('/', function (req, res, next) {
tno= req.param("tno");
new_json = {}
MongoClient.connect(mongoUrl, function (err, db) {
var cursor = db.collection('tns').find({"tno": tno});
cursor.toArray(function (err, docs) {
if (err) {
res.json({"error_server": "Error Occured,Please Try Again"})
}
mongoose.connect(mongoUrl);
console.log(tno)
var Rating = mongoose.model('Rating', ratingSchema);
Rating.aggregate(
[
{
$match: {"tno":tno}
},
{
"$group": {
"_id": "$tno",
"fr": { "$avg": "$fr" },
"cr": { "$avg": "$cr" },
"pr": { "$avg": "$pr" },
"tr": { "$avg": "$tr" },
}
}
],
function(err,results) {
if (err) throw err;
console.log("INSIDE AGG RESULTS")
temp_data=docs
console.log(results)
temp_data["ratings"]=results
console.log(temp_data)
res.json(temp_data)
}
);
});
}
});
我能够独立执行这两个查询但不能合并结果