Update data type from integer to decimal128 for all document having nest array in mongo db



  • Had a JSON structure which is having nested array and need to update all data type for that json structure

    [
      {
        student: [
          {
            name: {
              firstName: "Rahul",
              lastName: "khurana",
              Address: "xyz"
            },
            mark: [
              {
                maths: 19,
                english: 10,
                science: 20
              }
            ],
            subject: {
              subjectName: "Maths",
              subjectCode: 1
            }
          },
          {
            name: {
              firstName: "Shahrukh",
              lastName: "khan",
              Address: "xyz"
            },
            mark: [
              {
                maths: 45,
                english: 70,
                science: 100
              }
            ],
            subject: {
              subjectName: "Maths",
              subjectCode: 1
            }
          }
        ]
      }
    ]
    

    need to update maths datatype from integer to decimal128 tried query for updating data

    db.test.updateMany({},
       [
          {
             $set: {
    
            phone: {
               $map: {
                  input: "$phone",
                  as:"p",
          in:{address:{$map:{input:"$$p.phone",in:{$mergeObjects:["$this",{telephone:{$toDecimal:"$$this.telephone"}}]}}}}
               }
            }
         }
      }
    

    ]
    )

    thanks in advance



  • working with this query, thanks

    db.test.updateMany({},
       [
          {
             $set: {
    
            student: {
               $map: {
                  input: "$student",
                  as:"s",
          in:{name:"$$s.name",mark:{$map:{input:"$$s.mark",in:{$mergeObjects:["$$this",{maths:{$toDecimal:"$$this.maths"}}]}}},subject:"$$s.subject"}
               }
            }
         }
      }
    

    ]
    )




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2