3
我试图用另一个模型的数据填充模型。这两个模型是这样的:猫鼬查询填充查找元素的匹配id
var postSchema = mongoose.Schema({
_comments: { type: mongoose.Schema.Types.ObjectId, ref: 'Comment' },
type: String,
body: String,
});
var commentSchema = mongoose.Schema({
id_post: mongoose.Schema.Types.ObjectId,
body: String,
});
我想找到所有posts
与comments
有id_post
== _id
从创立帖子进行填充。这样的事情:
Post.find({}).populate({
path: '_comments',
select: 'body',
match: { post_id: Post._id }
options: { limit: 5 }
})
.exec(function (err, posts){...});
'postSchema'中的'_comments'应该包含类似'[{_id:Comment.id}]''的东西。或'[{id_post:Post._id}]' – TGeorge 2015-02-10 20:14:47
您可以在我的回答中找到修改后的postSchema,我刚刚以正确的方式添加了commentSchema以将其与Post相关联。 – 2015-02-10 20:25:21
在我的DATABASE里,一个'post'文件是什么样的? '_comments'应该包含什么? – TGeorge 2015-02-10 20:38:11