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

211
Vistas
mongo remove/pull grandchild item in deeply nested array

I have a deeply nested document of family members

[{
  "id": 1,
  "children" : [
    {
        "id" : 1,
        "grandChildren": [
            {
                "id" : 1,
                "toys": [
                    {
                        "id":1
                    }
                ]

            }
        ]
    },
    {
        "id" : 2,
        "grandChildren": [
            {
                "id" : 1,
                "toys": [
                    {
                        "id":1
                    }
                ]

            }
        ]
    }
  ]
}]

I'm told the toys are unique to the grandchildren, which are also unique, etc, but there's a chance they're not and I'd like to make absolutely sure when I remove them I'm only removing them for the specific grandchild of the specific child etc,

I have a query that can pull all toys matching the id I was wondering how I can be more specific and burrow down through the specific parents?

db.family.updateOne(
 {"id": "1"}, 
 {"$pull" : 
   { "children.$[].grandChildren.$[].toys" : 
     {"id" : "1"}
   }
 })

All help appreciated

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

What you want to do is use arrayFilters like so:

db.collection.updateOne({
  "id": "1"
},
{
  "$pull": {
    "children.$[child].grandChildren.$[grandChild].toys": {
      "id": "1"
    }
  }
},
{
  arrayFilters: [
    {
      "child.id": "1"
    },
    {
      "grandChild.id": "1"
    }
  ]
})

Mongo Playground

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