嵌套填充匹配函数使用父值的父值而不是父值

问题描述

我正在尝试通过使用 populate 来限制调用次数来优化脚本。

我发现填充匹配函数来自: http://thecodebarbarian.com/mongoose-5-5-static-hooks-and-populate-match-functions.html#populate-match-function

在猫鼬版本中: https://github.com/Automattic/mongoose/blob/master/History.md#550--2019-04-08

来自那个问题: https://github.com/Automattic/mongoose/issues/7397

基于所有这些,我试图将我的广告系列开始日期和结束日期与结果相匹配

let campaigns = await Campaign.find()
        .populate({
            path: "accommodationsList.accommodationId",populate: {
                path: "result",select: "_id startingDate endDate price",options: {sort: {_id: -1}}
            }
        }).lean();

工作正常,但我需要在最后一个填充中添加一个匹配项,例如: 匹配:活动 =>({开始日期:活动.开始日期,结束日期:活动.结束日期,错误:空}),

这里的campaign是一个accomodation,match函数只填充父值。

所以我想知道是否有办法将 Campaign.startingDate 传递给最后一个嵌套的填充匹配?

let campaigns = await Campaign.find()
            .populate({
                path: "accommodationsList.accommodationId",populate: {
                    path: "scrapedresult",match: campaign => ({ startingDate: campaign.startingDate,endDate: campaign.endDate }),options: { sort: { _id: 1 } }
                }
            }).lean();

这是我的广告系列架构:

const accommodationWithCompetitoRSSchema = new Schema(
{
    accommodationId: {
        type: Schema.ObjectId,ref:"accommodations",required: true
    }
});

const CampaignSchema = new Schema({
    accommodationsList: [
        accommodationWithCompetitoRSSchema
    ],startingDate: {
        type: Date,required: true
    },endDate:{
        type: Date,required: true
    }
});

我的住宿一:

const AccommodationSchema = new Schema({
    name: {
        type: String,required: true
    })
AccommodationSchema.virtual('result',{
    ref: 'Results',localField: '_id',foreignField: 'accommodationId',justOne: true
});

我的结果一

const ResultSchema = new Schema({
    accommodationId: {
        type: Schema.ObjectId,required: true,ref: "accommodations"
    },price: {
        type: Number,},endDate: {
        type: Date,});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)