Mongoose:我想填充一个属性,这是一个对象引用数组

时间:2015-10-08 01:23:51

标签: javascript angularjs node.js mongodb mongoose

var CommentsSchema = new mongoose.Schema({
created: Date,
user: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
body: String,
news: {type: mongoose.Schema.Types.ObjectId, ref: 'Newsletter'},
reply: [{ **//I want to populate the user into reply array**
    user: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
    body: String,
    created: Date
    }]
});

我正在填充我的评论,我还希望在回复数组的每个索引上填充用户。我是否必须在同一个电话会议或不同的电话会议上进行此操作?

router.post('/reply', auth, function(req, res) {
var reply = req.body;
reply.created = new Date();
reply.user = req.payload.id;
console.log(reply);
Comment.update({_id: reply.comment}, {$push: {reply: reply}})
.populate({
    path: 'user',
    model: 'User',
    select: 'username name facebook.photo images'
})
.populate({
    path: 'reply',
    model: 'user',
    select: 'username name facebook.photo images'
})
.exec(function(err, result) {
    if(err) return res.status(500).send({err: "Issues with server, re: reply"});
    if(!result) return res.status(400).send({err: "Could not post reply"});
    res.send(reply);
})
})

0 个答案:

没有答案