在MongoDB中使用两个不同的模式的populate()

我有两个MongoDB集合 – 评论

var mongoose = require('mongoose'); var CommentSchema = new mongoose.Schema({ body: String, author: String, upvotes: {type: Number, default: 0}, post: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile' } }); mongoose.model('Comment', CommentSchema); 

和用户

 var mongoose = require('mongoose'); var UserSchema = new mongoose.Schema({ userName: String, userJobRole: String, userEmail: String, userPhone: String, userTimeZone: String, post: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile' } }); mongoose.model('User', UserSchema); 

我想在我的获取请求中使用这些模式中的每个模式。 一个用户和一个来自这些模型的评论。

 router.get('/profiles/:profile', function(req, res, next) { req.post.populate('users', function(err, post) { if(err) { return next(err) } res.json(post); }); }); 

我只能弄清楚如何打电话。

Mongoose是否允许你从两个模式中填充?

为了填充多个path,可以将空格分隔的path名string传递给任何文档上的填充方法,如下所示:

 Story .find(...) .populate('fans _creator') // space delimited path names .exec() 

这是直接从Mongoose文档http://mongoosejs.com/docs/populate.html