How to rename a field name in array?

I have this structure and want to rename one particular field inside the array with many other fields.

    {
        "_id" : NumberLong(1),
        "Data" : [ 
            {
                "Payload" : {
                    "Body" : {
                        "quantity" : 1,
                        "productname" : "ABC"
                    }
                }
            }
        ]
    }

I want to rename the fieldname quantity to itemquantity.

I have tried something like this.

    db.getCollection('test').updateMany(
      {},
       [{
          $set: {
          Body: {
                $map: {
                   input: "$Data",
                   in: {
                            UnitInvoiced: "$$this.Payload.Body.quantity"
                       } 
                }
             }
          }
       }]
    )

Hi @Sudhesh_Gnanasekaran ,

I think the $map approach is a correct one if the collection is small. You will need to do a $cond with the correct condition to replace the specific field with a new expression.

json - MongoDB rename database field within array - Stack Overflow.

The above thread have multiple examples on how to do this change. Perhaps think about scripting or having an application to migrate the data as it recognise old field values into new.

Thanks
Pavel