【发布时间】:2017-10-08 03:02:59
【问题描述】:
我有一个Post,它的author 属性通过使用ref 设置为User。假设User 中有一个country 字段,如何使Post 架构具有country 属性以及填充后来自User.country 的值。
const Post = new Schema({
text: String,
author: {
type: ObjectId,
ref: 'User'
},
// How to set the virtual property country which is from User.country?
});
我知道猫鼬中有一个Populate Virtuals,但它似乎只是populating 的另一种方式,它不会获取其中一个属性,而是获取整个引用记录。还是我弄错了?
我知道我可以从Post.author.country 引用country,但我也想要Post.country。
如何解决这个问题?
我可以在架构级别执行此操作吗?
【问题讨论】: