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