【发布时间】:2015-09-08 07:46:23
【问题描述】:
我有一个文章架构,用于用户在我的网站上发布的文章。它引用了用户集合:
var ArticleSchema = new Schema({
title: { // NO MARKDOWN FOR THIS, just straight up text for separating from content
type: String,
required: true
},
author: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});
我想在所有 find/findOne 调用上添加一个 post hook 来填充参考:
ArticleSchema.post('find', function (doc) {
doc.populate('author');
});
由于某种原因,挂钩中返回的文档没有填充方法。我是否必须使用 ArticleSchema 对象而不是在文档级别进行填充?
【问题讨论】:
-
编辑:我们已经离开了 mongo 来做这样的事情。对于大多数生产应用程序来说,使用关系数据库要容易得多。我们使用 postgresql。
标签: node.js mongodb mongoose middleware mongoose-populate