0

In mongo DB I have a collection dealing with buildings ("centros") that contain devices ("dispositivos") and those devices contain sensors ("sensores"). I am trying to update one of those sensors. Buildings are identified by their "_id", devices by their "uid" and sensors by their "variable name" ("variable"). I have already tried following some examples found on StackOverflow, but have not succeeded.

This is my approximation:

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": "intensidad"
    }
  ]
})

But it gives me the following error:

fail to run update: multiple write errors: [{write errors: [{Error parsing array filter :: caused by :: Expected a single top-level field name, found 'sensor' and 'dispositivo'}]}, {<nil>}]

There you have a live sample: https://mongoplayground.net/p/KrWw2WxkSch

This is my collection sample:

[
  {
    "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"
          }
        ]
      }
    ]
  }
]

Thanks JOE for the help. That is the solution:

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-064386055555"
    },
    {
      "sensor.variable": "intensidad"
    }
  ]
})

1 Answer 1

1

Each array filter should be its own object in the array, like

 arrayFilters: [
    {
      "dispositivo.uid": "e898b585-d855-4017-9fe4-064386kkkkkk"
    },
    {
      "sensor.variable": "intensidad"
    }
  ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.