【发布时间】:2015-07-06 18:43:55
【问题描述】:
我有两个 MongoDB 集合 - cmets
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);
我想在我的 get 请求中为这些模式中的每一个使用填充。一个用于用户,一个用于这些模型的 cmets。
router.get('/profiles/:profile', function(req, res, next) {
req.post.populate('users', function(err, post) {
if(err) { return next(err) }
res.json(post);
});
});
我只能弄清楚如何调用一个。
Mongoose 是否允许您从两个模式中进行填充?
【问题讨论】: