Compare 2 fields in arrayFilters

Hi,

I would like to compare two fields inside the arrayFilters. In particulare, I have the following modeling:

services: [
   {
    name: string
    total: number
    current: number
   },
   ...
]

I would like to perform the following operation:

let delta: number = 1
let serviceName: string = "serviceName"
let filterOp = {
   _id: ObjectId("...")
}
let updateOp = {
  $inc: {
       "services.$[service].current": delta
    }
}
let arrayFiltersOp = [
   {
   "service.name": serviceName //This is just to filter by serviceName and it works.
   "service.total": {$gte: "$service.current" + delta} //This doesn't work!!!
   }
]

db.find(filterOp).arrayFilters(arrayFiltersOp).updateOne(updateOp)

Hi @Matteo_Tarantino ,

Yes I don’t think that with array filter you will be able to compare a calculated value from another field.

You will have to perform an aggregation pipeline update and use $map to traverse arrays and calculate fields:
https://www.mongodb.com/docs/manual/tutorial/update-documents-with-aggregation-pipeline/#:~:text=Using%20the%20aggregation%20pipeline%20allows,of%20another%20field(s).

If you have troubles writing a specific one let me know

Thanks
Pavel