【发布时间】:2014-08-16 09:19:40
【问题描述】:
我的 MongoDB 中有这个设置
项目:
title: String
comments: [] // of objectId's
评论:
user: ObjectId()
item: ObjectId()
comment: String
这是我的 Mongoose 架构:
itemSchema = mongoose.Schema({
title: String,
comments: [{ type: Schema.Types.ObjectId, ref: 'comments' }],
});
Item = mongoose.model('items', itemSchema);
commentSchema = mongoose.Schema({
comment: String,
user: { type: Schema.Types.ObjectId, ref: 'users' },
});
Comment = mongoose.model('comments', commentSchema);
这是我与 cmets 一起获得物品的地方:
Item.find({}).populate('comments').exec(function(err, data){
if (err) return handleError(err);
res.json(data);
});
如何使用其各自的用户填充 cmets 数组?既然每条评论都有一个用户ObjectId()?
【问题讨论】:
-
您可以分两个阶段进行操作,如该问题的已接受答案所示:stackoverflow.com/questions/19222520/…
标签: javascript node.js mongodb mongoose