使用mongoose findByIdAndUpdate更新父对象中对象嵌套数组的一部分

rsl1atfo  于 2023-03-12  发布在  Go
关注(0)|答案(1)|浏览(154)

这是mongoose模型的一部分

const mandateSchema = new mongoose.Schema({
    _id: {
        type: String,
        required: true,
    },
    customer: {
        type: String,
        required: true,
        index: true
    },
    tariffs: {
        gas: {
            product_name: {
                type: String
            },
            code: {
                type: String
            },
            default_coefficients: {
                type: Boolean,
                default: true
            },
            assigned_eans: [{
                eanID: {
                    type: String,
                    ref: Ean,
                },
                status: {
                    type: String,
                    required: false
                },
                start_date: {
                    type: Number,
                },
                end_date: {
                    type: Number,
                },
                segment: {
                    type: String,
                    enum: ['b2c', 'soho', 'b2b', 'corporate', 'industry', 'direction']
                },
                stop_reason: {
                    type: String
                },
                stop_note: {
                    type: String
                },
            }]
        },
        electricity: {
            product_name: {
                type: String
            },
            code: {
                type: String
            },
            default_coefficients: {
                type: Boolean,
                default: true
            },
            assigned_eans: [{
                eanID: {
                    type: String,
                    ref: Ean,
                },
                start_date: {
                    type: Number,
                },
                end_date: {
                    type: Number,
                },
                segment: {
                    type: String,
                    enum: ['b2c', 'soho', 'b2b', 'corporate', 'industry', 'direction']
                },
                stop_reason: {
                    type: String
                },
                stop_note: {
                    type: String
                },
            }]
        }
    },
    start_date: {
        type: mongoose.Schema.Types.Mixed,
        required: false
    },
    end_date: {
        type: mongoose.Schema.Types.Mixed,
        required: false
    },
    end_from_ip: {
        type: String,
        required: false
    },
    stop_reason: {
        type: String,
        required: false
    },
    stop_reason_description: {
        type: String,
        required: false
    },
    sign_date: {
        type: Number,
        required: false
    },
    sign_token: {
        type: String,
        required: false
    },
    sign_from_ip: {
        type: String,
        required: false
    }
});

基于给定的_ideanID,我正在寻找一种方法来更新对象,该对象与新的statusend_datestop_reason和可选的stop_note匹配,而不会丢失已经填充的start_dateeanID
EnergyMandate.findByIdAndUpdate(energyMandateId的开始非常简单。但是对于更新部分,我不知道如何找到匹配的对象。我已经检查了Stackoverflow上的其他解决方案,但在我的示例中,首先是在tarias.gas和tarias.electricity之间进行拆分,然后搜索匹配的eanID。

vq8itlhq

vq8itlhq1#

可以像这样使用array filters

EnergyMandate.findByIdAndUpdate(energyMandateId,
{
  $set: {
    "tariffs.gas.assigned_eans.$[elem].status": yourNewStatus,
    "tariffs.gas.assigned_eans.$[elem].end_date": yourNewEndDate,
    "tariffs.gas.assigned_eans.$[elem].stop_reason": yourNewStopReason,
    "tariffs.gas.assigned_eans.$[elem].stop_note": yourNewStopNote
  }
},
{
  arrayFilters: [
    {
      "elem.eanID": yourEanId
    }
  ]
})

示例here

相关问题