Home > Enterprise >  mongodb: decrease all values (in -1.5) that match with specific filters
mongodb: decrease all values (in -1.5) that match with specific filters

Time:05-12

I'm trying to decrease all "temperature values" that match with "sensor_id":2 and "location_id":1, I need to decrease them in - 1.5.

this is the collection:

    db.testsensores.insert([{"timestamp":"2020-07-01T00:00:00Z", "fecha_instalacion":"2020-05-28T11:30:00Z","sensor_id":1,"location_id":2, "Coordenadas":[37.409311, -5.949939], "Ubicacion":"Sevilla", "medidas":[{"tipo_medida":"Temperatura","valor":22.08,"unidad":"ºC"},{"tipo_medida":"Humedad_relativa","valor":34.92,"unidad":"%"}]},
                                                                 
    {"timestamp":"2020-07-01T10:00:00Z", "fecha_instalacion":"2020-05-28T11:30:00Z","sensor_id":2,"location_id":2, "Coordenadas":[37.409311, -5.949939], "Ubicacion":"Sevilla", "medidas":[{"tipo_medida":"Emision_CO2","valor":2.055,"unidad":"gCO2/m2"},{"tipo_medida":"Consumo_electrico","valor":0.00269,"unidad":"kWh/m2"}]},
                                                                     
    {"timestamp":"2020-07-10T00:00:00Z", "fecha_instalacion":"2020-05-28T11:30:00Z","sensor_id":1,"location_id":2, "Coordenadas":[37.409311, -5.949939], "Ubicacion":"Sevilla", "medidas":[{"tipo_medida":"Temperatura","valor":21.12,"unidad":"ºC"},{"tipo_medida":"Humedad_relativa","valor":37.7,"unidad":"%"}]},
                                                                 
    {"timestamp":"2020-07-10T00:00:00Z", "fecha_instalacion":"2020-05-28T11:30:00Z","sensor_id":2,"location_id":2, "Coordenadas":[37.409311, -5.949939], "Ubicacion":"Sevilla", "medidas":[{"tipo_medida":"Emision_CO2","valor":2.102,"unidad":"gCO2/m2"},{"tipo_medida":"Consumo_electrico","valor":0.00272,"unidad":"kWh/m2"}]},
                                                                     
    {"timestamp":"2020-07-01T10:00:00Z", "fecha_instalacion":"2020-05-25T10:30:00Z","sensor_id":1,"location_id":1, "Coordenadas":[41.638597,4.740186], "Ubicacion":"Valladolid", "medidas":[{"tipo_medida":"Temperatura","valor":16.61,"unidad":"ºC"},{"tipo_medida":"Humedad_relativa","valor":83.74,"unidad":"%"}]},
                                                                     
    {"timestamp":"2020-07-01T00:00:00Z", "fecha_instalacion":"2020-05-25T10:30:00Z","sensor_id":2,"location_id":1, "Coordenadas":[41.638597,4.740186], "Ubicacion":"Valladolid", "medidas":[{"tipo_medida":"Emision_CO2","valor":1.572,"unidad":"gCO2/m2"},{"tipo_medida":"Consumo_electrico","valor":0.00188,"unidad":"kWh/m2"}]},
                                                                     
    {"timestamp":"2020-07-10T00:00:00Z", "fecha_instalacion":"2020-05-25T10:30:00Z","sensor_id":1,"location_id":1, "Coordenadas":[41.638597,4.740186], "Ubicacion":"Valladolid", "medidas":[{"tipo_medida":"Temperatura","valor":15.75,"unidad":"ºC"},{"tipo_medida":"Humedad_relativa","valor":83.08,"unidad":"%"}]},
                                                                     
    {"timestamp":"2020-07-10T00:00:00Z", "fecha_instalacion":"2020-05-25T10:30:00Z","sensor_id":2,"location_id":1, "Coordenadas":[41.638597,4.740186], "Ubicacion":"Valladolid", "medidas":[{"tipo_medida":"Emision_CO2","valor":1.626,"unidad":"gCO2/m2"},{"tipo_medida":"Consumo_electrico","valor":0.00146,"unidad":"kWh/m2"}]}])

And this is what I'm trying to do:

    db.testsensores.updateMany(
      {"sensor_id":2, "location_id":1,"medidas.tipo_medida":"temperatura"},
      {$set:{"medidas.valor":{"medidas.valor"-1.5}}}
    )

it is not working, could you please guide me in how to do it work? really appreciate it.

CodePudding user response:

Use arrayFilters to match and update array elements. Note that in your sample dataset, none of the document is matched, so I modified the find criteria a bit to demonstrate the query.

db.collection.update({
  "sensor_id": 1,
  "location_id": 2
},
{
  $inc: {
    "medidas.$[element].valor": -1.5
  }
},
{
  arrayFilters: [
    {
      "element.tipo_medida": "Temperatura"
    }
  ],
  multi: true
})

Here is the Mongo playground for your reference.

  • Related