Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda