Home > Software engineering >  mongodb update with conditions
mongodb update with conditions

Time:05-02

I have my orders collection as

[
  {
    _id:'134',
    items:[
        {
            _id:'itemId1'   
            quantity: 2,
            price: 100,,
            couponsApplied:[]
        },
        {
            _id:'itemId2'   
            quantity: 2,
            price: 200,,
            couponsApplied:[]
        }
    ]
   } 
 ]

I want to apply 5% discount to each items above. i.e on item _id:'itemId1', I want to push I want to push to couponsApplied as

{
    couponAmount: 10, // quantity*price*percentage/100. which is 2*100*5/100 
}

& Similarly on _id:'itemId2', I want to push to couponsApplied as

{
    couponAmount: 20, 
}

So, after the update operation it should look like

[
  {
    _id:'134',
    items:[
        {
            _id:'itemId1'   
            quantity: 2,
            price: 100,,
            couponsApplied:[
             {
              _id:'100',
              couponAmount: 10
              }               
            ]
        },
        {
            _id:'itemId2'   
            quantity: 2,
            price: 200,,
            couponsApplied:[
              {
               _id:'1002'
               couponAmount: 20, 
              }
            ]
        }
    ]
   } 
 ]

I have tried aggregate update with $mul and $sum but no luck Please help!

CodePudding user response:

A bit long update query.

  1. $set - Update the items array field.

    1.1. $map - Iterates with items array and returns an array.

    1.1.1. $mergeObjects - Merge current object with couponsApplied array field.

    1.1.1.1. $concatArrays - Combine couponsApplied array with the result of 1.1.1.1.1.

    1.1.1.1.1. An array with new document contains couponAmount field.

db.collection.update({},
[
  {
    $set: {
      "items": {
        $map: {
          input: "$items",
          in: {
            $mergeObjects: [
              "$$this",
              {
                couponsApplied: {
                  $concatArrays: [
                    "$$this.couponsApplied",
                    [
                      {
                        couponAmount: {
                          $multiply: [
                            "$$this.price",
                            "$$this.quantity",
                            {
                              $divide: [
                                5,
                                100
                              ]
                            }
                          ]
                        }
                      }
                    ]
                  ]
                }
              }
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

CodePudding user response:

try this:

db.collection.update({},
[
  {
    "$set": {
      "items": {
        $map: {
          input: "$items",
          as: "item",
          in: {
            _id: "$$item._id",
            quantity: "$$item.quantity",
            price: "$$item.price",
            couponsApplied: {
              $concatArrays: [
                "$$item.couponsApplied",
                [
                  {
                    couponAmount: {
                      $divide: [
                        {
                          $multiply: [
                            "$$item.quantity",
                            "$$item.price",
                            5
                          ]
                        },
                        100
                      ]
                    }
                  }
                ]
              ]
            }
          }
        }
      }
    }
  }
])
  • Related