我在數據庫中使用mLab和節點js.mig中使用mongoose。我正在使用不應該引起任何問題的swagger。我有以下模式。當用戶請求時,如果(查看=真)在查詢中,我需要一起返回電影和查看。一部電影可能有多個評論。首先,我必須在數據庫中找到所有電影。當我找到電影時,我必須仔細閱讀其中的每一個,在另一個數據庫中查找任何評論,並以某種方式將它們附加到電影中。我需要將所有電影歸還到一個包中,因爲它將用於getAll函數。無論我做什麼,只會在沒有評論的情況下回覆電影。 組合兩個表/集合的mongodb/mongoose聚合
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
movie: {type: String, required: true},
reviewer: {type: String, required: true},
rating: {type: Number, required:true, min: 1, max: 5},
text: {type: String, required: true},
})
var movieSchema = new mongoose.Schema({
Title: {type: String, required: true, unique: true},
YearReleased: {type: Number, required: true},
Actors: [{
Name: {type: String, required: true}
}]
})
。
movies.forEach(function(movie, index){
// this call is asynchronous, res.send will run before the callback
db.Reviews.find({movie:movies[index].Title}, function (err, review) {
if(err) throw {err:err}
movies[index].Review = review // I am trying to populate each movie with reviews
});
});
res.send({Movielist: movies});
您可以使用承諾等待結果:前DB審查結果被接收
function getAll(req,res,next){
db.Movies.find({},function(err,movies){
if(err) throw {err:err};
if(req.swagger.params.review.value === false)
res.send({Movielist: movie});
else {
movies.forEach(function(movie, index){
db.Reviews.find({movie:movies[index].Title}, function (err, review) {
if(err) throw {err:err}
movies[index].Review = review // I am trying to populate each movie with reviews
});
});
res.send({Movielist: movies});
}
});
}