1




db.Hours.aggregate(
  {$addFields: {TrueAmbientTemp: { {$add : [-8 , {$multiply : ["$AmbientTemp" , 47]}]}}}}
)

I am trying to add a new field TrueAmbientTemp which is a calculation field. The above give an error.

BoogieMan
  • 71
  • 1
  • 1
  • 6

1 Answers1

0

You miss some semicolons.

db.collection.aggregate({
  $addFields: {
    TrueAmbientTemp: {
      $add: [
        -8,
        {
          $multiply: [
            "$AmbientTemp",
            47
          ]
        }
      ]
    }
  }
})

Example:mongoplayground

update:you can change update to updateMany

db.collection.update({},
[
  {
    $addFields: {
      TrueAmbientTemp: {
        $add: [
          -8,
          {
            $multiply: [
              "$AmbientTemp",
              47
            ]
          }
        ]
      }
    }
  }
])

Example:mongoplayground

YuTing
  • 6,555
  • 2
  • 6
  • 16