Database
[{"vendor":"Vendor 1","productsInOrder":[{"orderLimit":"4","productId":"newProductId","amount":"3"},{"orderLimit":"4","productId":"62e8e2dfcf421fdd53e3fd45","amount":"4"},],},{"vendor":"Vendor 2","productsInOrder":[]}]
Query
db.collection.update({"vendor":"Vendor 1"},[{$set:{productsInOrder:{$cond:[{$in:["newProductId",{$ifNull:["$productsInOrder.productId",[]]}]},/** if product exists*/{$map:{input:"$productsInOrder",in:{$cond:[{$and:[{$eq:["$$this.productId","newProductId"]},{$lt:[{$toInt:"$$this.amount"},{$toInt:"$$this.orderLimit"}]}]},{$mergeObjects:["$$this",{amount:{$sum:[1,{$toInt:"$$this.amount"}]}}]},/** do nothing*/"$$this",]},}},/** product does not exist*/{$concatArrays:[{$ifNull:["$productsInOrder",[]]},[{amount:1,orderLimit:1,productId:"newProductId"}]]}]}}},],{upsert:true})