skip to Main Content

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!

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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
                          ]
                        }
                      }
                    ]
                  ]
                }
              }
            }
          }
        }
      }
    ])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search