这是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
}
});
基于给定的_id
和eanID
,我正在寻找一种方法来更新对象,该对象与新的status
、end_date
、stop_reason
和可选的stop_note
匹配,而不会丢失已经填充的start_date
和eanID
。EnergyMandate.findByIdAndUpdate(energyMandateId
的开始非常简单。但是对于更新部分,我不知道如何找到匹配的对象。我已经检查了Stackoverflow上的其他解决方案,但在我的示例中,首先是在tarias.gas和tarias.electricity之间进行拆分,然后搜索匹配的eanID。
1条答案
按热度按时间vq8itlhq1#
可以像这样使用array filters:
示例here