I want push new object to nested array if object with field ‘Cat’=1 not exists. Or if object with ‘Cat’=1 field exists then increment Up field in this object.
WOW thx.
But why so hardy for easzy operation. amazing… with $reduce, $$this, $concatArrays, $$value
Intresting can we optimize for more light code query
The operation uses an aggregation instead of an update - and there is some logic involved (find-and-modify-or-insert), hence all the code. And, this doesn’t allow the update operators like, $set, $inc, $push, etc.
The $set used above is not an operator - it is an aggregation pipeline stage. The operators used within aggregation are different, are easy to use and can derive complex programming logic using objects, dates, arrays, conditions, strings, etc - which is not possible with direct update.
Without using aggregation, we can end up using two operations (and lighter code) instead of one operation. As such this update operation is atomic.