I am trying to update all the documents of a collection to introduce two new elements in the array of measurements (medidas) but only in documents with "sensor_id: 2". The collection, db.datos_sensores2, has documents within like:
{
"_id" : ObjectId("609c2c2d420a73728827e87f"),
"timestamp" : ISODate("2020-07-01T02:15:00Z"),
"sensor_id" : 1,
"location_id" : 1,
"medidas" : [
{
"tipo_medida" : "Temperatura",
"valor" : 14.03,
"unidad" : "ºC"
},
{
"tipo_medida" : "Humedad_relativa",
"valor" : 84.32,
"unidad" : "%"
}
]
}
{
"_id" : ObjectId("609c2c2d420a73728827e880"),
"timestamp" : ISODate("2020-07-01T02:15:00Z"),
"sensor_id" : 2,
"location_id" : 1,
"medidas" : [
{
"tipo_medida" : "Emision_CO2",
"valor" : 1.67,
"unidad" : "gCO2/m2"
},
{
"tipo_medida" : "Consumo_electrico",
"valor" : 0.00155,
"unidad" : "kWh/m2"
}
]
}
I wrote this:
db.datos_sensores2.update(
{"_id":0, "location_id":1, "sensor_id": 2},
{$push:{"$medidas":{$each:[
{"precio_kWh":0.102,"unidad":"€/kWh"},
{"superficie":450,"unidad":"m2"}]}}},{multi:true})
But nothing happen...
the first query of update has _id:0
and couldn't find doc
another thing $medidas
in update query don't need $
and error accoured
use this update
db.collection.update({
"location_id": 1,
"sensor_id": 2
},
{
$push: {
"medidas": {
$each: [
{
"precio_kWh": 0.102,
"unidad": "€/kWh"
},
{
"superficie": 450,
"unidad": "m2"
}
]
}
}
},
{
multi: true
})