I have 5 documents in MongoDB collection which stores the temperature values in Celsius. I need to create a query so that I can convert each value into Fahrenheit. The document is in below format:
{
"_id" : ObjectId("5c3eecfb5c4d120a7c4f5deb"),
"sensorType" : "temperature",
"sensorData" : [
{
"time" : "2018-08-31T19:32:15Z",
"value" : 16
},
{
"time" : "2018-08-31T19:47:21Z",
"value" : 18
},
{
"time" : "2018-08-31T20:02:21Z",
"value" : 23
},
{
"time" : "2018-08-31T20:17:21Z",
"value" : 19
},
It has time and the value, I need to convert the value in complete sensorData.
To resolve this, I started with the below query:
db.sensordata.updateMany(
{ sensorType: "temperature" },
{
$set: { "sensorData.$[].value": 8*2 },
}
)
In the above query, I have selected sensorType as temperature because the collection contains other sensorType as well. In $set, I am just using a simple formula 8*2 just to check if the query works or not. By running the above query, all the values are updated to 16, but I am not able to first fetch the current value and then do the math to convert it into fahrenheit. Below is the formula which we can use:
celsius * 9/5 + 32
I tried with below query as well but it gave me error:
db.sensordata.updateMany(
{ sensorType: "temperature"},
{
$set:{"sensorData.$[].value": "($sensorData.value * 9/5) + 32"}
})
But instead of converting it is saving the value as ($sensorData.value * 9/5) + 32. Can anyone please help me in creating this query?