【发布时间】:2015-11-11 16:39:25
【问题描述】:
我有三个模型:用户、帖子和评论
var User = new Schema({
name: String,
email: String,
password: String // obviously encrypted
});
var Post = new Schema({
title: String,
author: { type: Schema.ObjectId, ref: 'User' }
});
var Comment = new Schema({
text: String,
post: { type: Schema.ObjectId, ref: 'Post' },
author: { type: Schema.ObjectId, ref: 'User' }
});
我需要获取用户评论过的所有帖子。
我知道这应该是一个非常简单和常见的用例,但现在我想不出一种方法来在没有多次调用和手动迭代结果的情况下进行查询。
我一直在考虑将comments 字段添加到Post 架构(我希望避免使用)并制作如下内容:
Post.find()
.populate({ path: 'comments', match: { author: user } })
.exec(function (err, posts) {
console.log(posts);
});
在不修改我的原始架构的情况下有任何线索吗?
谢谢
【问题讨论】: