Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
mongoDB update with arrayFilters not working

I have the following doc structure (simplified) in mongo:

{
  "_id":"5e30208675b5400cb0894c52",
  "locations": [
    {
      "name": "Pleasure Gardens",
      "id": 618,
      "areas": [
        {
          "name": "Koi Pond",
          "area_id": 159,
          "is_active": true,
        },
        ...other areas
      ],
      ...other locations 
    }
  ]
}

I'm trying to set the is_active field in the areas array to false where the id of the location is 618 and the the area_id of the area is 159.

I'm doing the following:

const db = await connectDb();

return await db
 .collection('test')
 .updateOne(
   {"locations.areas.area_id": 159},
   { $set: {"locations.$[l].areas.$[a].is_active": false }},
   { arrayFilters: [{ "l.id": 618 }, { "a.area_id": 159 }] }
);

But I'm getting:

MongoError: cannot use the part (locations of locations.$[l].areas.$[a].is_active) to traverse the element

I've checked other responses and as far as I can tell the syntax is correct and I'm also using a mongodb version that supports the arrayFilters method ("mongodb": "^3.6.3") in nodejs, so why is it being so cruel to me? WHYYYY?!?!

UPDATE

After reading through the comments I realised that although I have version 3.6.3 of the mongodb npm package installed, the version of the database itself is actually 3.0.14

This means that the arrayFilters method is not supported and I will probably have to take a 2 step approach to solve this problem:

  1. query mongodb to find the document:
  • store result in a variable
  • update the variable in the code itself with (change is_active to false)
  1. use an update query to substitute the whole document in mongo with the document stored in variable (rather than just updating the desired area)
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

For me is working correctly(mongod/mongos version 4.4.3):

mongos> db.l.find().pretty()
{
"_id" : "5e30208675b5400cb0894c52",
"locations" : [
    {
        "name" : "Pleasure Gardens",
        "id" : 618,
        "areas" : [
            {
                "name" : "Koi Pond",
                "area_id" : 159,
                "is_active" : true
            }
        ]
    }
]

}

 mongos> db.l.updateOne( {"locations.areas.area_id": 159} , { $set:{"locations.$[loc].areas.$[are].is_active":false }}, { arrayFilters: [{ "loc.id": 618 }, { "are.area_id": 159 }] }     )
// { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

mongos> db.l.find({ "locations.areas.area_id": 159  } ,{"locations.areas.is_active":1  })
// { "_id" : "5e30208675b5400cb0894c52", "locations" : [ { "areas" : [ { "is_active" : false } ] } ] } 
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda