【问题标题】:Mongoose virtual population when foreignField is an object in the model当foreignField是模型中的对象时的猫鼬虚拟种群
【发布时间】:2019-08-30 03:17:31
【问题描述】:

我正在尝试使用 mongoose 填充虚拟属性,在我的模型中它是一个对象。我不知道如何定位foreignField。 我找不到任何例子,我会很感激任何帮助:)

喜欢的型号:

owner: {
        user: {
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: 'User'
        },
        status: {
            type: String,
            default: null // In progress, Completed
        }
    },

虚拟字段所在的用户模型:

userSchema.virtual('favours', {
    ref: 'Favour',
    localField: '_id',
    foreignField: ?????
})

【问题讨论】:

    标签: mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    解决了!!

    userSchema.virtual('favours', {
        ref: 'Favour',
        localField: '_id',
        foreignField: 'owner.user'
    })
    

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
    猜你喜欢
    • 2016-06-18
    • 2016-07-31
    • 2019-02-10
    • 2013-09-13
    • 2021-05-10
    • 1970-01-01
    • 2020-12-04
    • 2019-09-13
    • 2017-06-02
    相关资源
    最近更新 更多