javascript - 填充嵌套模式 Mongoose
问题描述
我有这个用户模型:
const userSchema = new Schema({
_id: {
type: Schema.Types.ObjectId,
required: true
},
name: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
notification: {
experiment_id: {
type: Schema.Types.ObjectId,
ref: "Experiment",
required: false
},
seen: {
type: Boolean,
required: true,
default: false
}
}
});
而这个实验模型:
const experimentSchema = new Schema(
{
_id: {
type: Schema.Types.ObjectId,
required: true
},
name: {
type: String,
required: true
},
description: {
type: String,
required: true,
default: "No Description"
},
author_id: {
type: Schema.Types.ObjectId,
ref: "User",
required: true
}
);
我正在尝试从用户填充通知中的实验 ID。从这个填充中,我也想填充 author_id。我已经看到了一些类似我在下面所做的代码,但我没有成功。
我正在尝试这个:
User.find(
{
_id: req.params.currentUserId
},
"notification"
)
.populate({ path: "experiment_id", populate: { path: "author_id" } })
.exec((err, notif) => {
});
解决方案
我通过在路径中添加notification.experiment_id来修复它
User.find(
{
_id: req.params.currentUserId
},
"notification"
)
.populate({ path: "notification.experiment_id", populate: { path: "author_id" } })
.exec((err, notif) => {
});
推荐阅读
- android - java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 错误:查看日志了解详细信息
- events - 谷歌分析事件仅在一段时间内有效
- linux - 如何根据部分输入 grep 完整的单词?
- python - 如何使用peewee ORM查找mysql表中存在的用户?
- angular - 我想将角度 5 中的 img/icon 从一个组件拖放到另一个组件
- sql - 将查询 Oracle 转换为 Postgresql
- symfony - Sonata Admin - 数据网格过滤器中的 DISTINCT
- python-3.x - 在python中逐列附加二进制值
- javascript - 未从 Web 服务中的后端获得预期响应
- python - 使用正则表达式提取子字符串