mongodb - 从父模式填充字段
问题描述
我有一个事务架构如下:
const transactionSchema = mongoose.Schema( {
amount: Number,
order: {
type: mongoose.Schema.ObjectId,
ref: 'Order'
}
},
{
toJson: { virtuals: true },
toObject: { virtuals: true }
}
);
以及一个具有如下驱动字段的订单模式(每笔交易都与一个订单相关)
const orderSchema = mongoose.Schema(
{
title: String,
driver: {
type: mongoose.Schema.ObjectId,
ref: 'User'
})
当我查询(查找)事务时,我需要从用户架构中提取驱动程序名称我的用户架构如下:
const userSchema = new mongoose.Schema({ name: String, })
这怎么可能?我按如下方式尝试了虚拟,但它没有用
transactionSchema.virtual('driverName', {
ref: 'User',
foreignField: 'name', //name of reference field in Review model
localField: 'driver' //name of reference in local Model
});
解决方案
我发现这可以根据猫鼬文档通过深度填充(跨多个级别填充)来解决
推荐阅读
- git - git-log 缺少取消更改的合并提交
- python - 用最大值打印numpy数组的索引
- python - 如果当天是工作日,则获取当前日期,否则获取最近工作日的日期(即星期五)
- javascript - 如何在 Knex.js 迁移中使用“金钱”数据类型?
- c# - 我有一个关于 ldap 获取 memberOf 值的问题
- javascript - 不要在浏览器或标签关闭时销毁 Vue-Cookie
- spring - 我们如何在 where 块中使用 hashmap 作为变量
- javascript - Sails.js 项目使用 passport.js 不会重定向到 twitter 登录
- perl - 列出目录中的文件并存储在数组中。该数组可以在循环外访问
- azure-application-insights - 为 AppInsight RestAPI 上下文设置时间跨度