简体   繁体   中英

How to update an object inside an array, inside another array in Spring + Mongo

I have a Spring + MongoDB project. I have a Collections about buildings ("centros". identified by _id), which have inside devices("dispositivos". identified by uid) which have inside sensors("sensores" identified by "variable"). And I am trying to update 1 sensor.

This is what I want to achieve in mongoDB:

db.collection.update({
  "uid": "e898b585-d855-4017-9fe4-0644360f9d1b"
},
{
  "$set": {
    "dispositivos.$[dispositivo].sensores.$[sensor]": {
      "variable": "Plutonio",
      "unidad": "yuyuyuyuuyu"
    }
  }
},
{
  "multi": false,
  "upsert": false,
  arrayFilters: [
    {
      "dispositivo.uid": "e898b585-d855-4017-9fe4-064386kkkkkk",
      
    },
    {
      "sensor.variable": "caudal"
    }
  ]
})

I have tried to adapt that to Spring, but have not succeeded. It always gives me some kind of error. This is what I have so far, but it doesn't work.

    public UpdateResult upsertSensor(String idCentro, String idDispositivo, String varSensor, JsonNode sensorBody) {

        ObjectId objectId = new ObjectId(idCentro);
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(objectId));

        Update update = new Update();
        update.filterArray(new Criteria("dispositivo.uid").is(idDispositivo).and("sensor.variable").is(varSensor));
        update.set("dispositivos.$[dispositivo].sensores.$[sensor]", sensorBody);
        
        return mongoTemplate.updateFirst(query, update, "centros");
    }

This is a sample document:

[
  {
    "uid": "e898b585-d855-4017-9fe4-0644360f9d1b",
    "nombre": "Colegio Rio Piles",
    "tipo": "Colegio",
    "direccion": {
      "calle": "Paseo Dr. Fleming, 1109",
      "codigoPostal": "33204",
      "municipio": "Gijon",
      "provincia": "Asturias",
      "ubicacion": {
        "latitud": "43.5351406",
        "longitud": "-5.6345379"
      }
    },
    "horario": {
      "apertura": "09:00",
      "cierre": "22:00"
    },
    "dispositivos": [
      {
        "uid": "e898b585-d855-4017-9fe4-064386055555",
        "descripcion": "",
        "tipo": "ANALIZADOR_RED",
        "adquisicion": "30s",
        "sensores": [
          {
            "variable": "voltaje",
            "unidad": "V"
          },
          {
            "variable": "intensidad",
            "unidad": "A"
          }
        ]
      },
      {
        "uid": "e898b585-d855-4017-9fe4-064386kkkkkk",
        "descripcion": "",
        "tipo": "CONTADOR_AGUA",
        "adquisicion": "30s",
        "sensores": [
          {
            "variable": "caudal",
            "unidad": "l/s"
          }
        ]
      }
    ]
  }
]

The question is how to take the mongoDB query that works to Java?

That was my solution:

    public UpdateResult upsertSensor(String idCentro, String idDispositivo, String varSensor, JsonNode sensorBody) {

        ObjectId objectId = new ObjectId(idCentro);
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(objectId));

        Update update = new Update();
                    
        update.filterArray(new Criteria("dispositivo.uid").is(idDispositivo));
        update.filterArray(new Criteria("sensor.variable").is(varSensor));  
        
        update.set("dispositivos.$[dispositivo].sensores.$[sensor]", sensorBody);
        
        return mongoTemplate.updateFirst(query, update, "centros");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM