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

463
Visualizações
how to remove item in nested array on mongo

I need to remove item in nested array, following is my document

{
    "_id" : ObjectId("58760d8caa30c585ef8d3beb"),
    "results" : [ 
        {
            "item" : "A",
            "score" : 5.0,
            "answers" : [ 
                {
                    "q" : 1.0,
                    "a" : 4.0
                }, 
                {
                    "q" : 1.0,
                    "a" : 5.0
                }, 
                {
                    "q" : 2.0,
                    "a" : 6.0
                }
            ]
        }, 
        {
            "item" : "B",
            "score" : 8.0,
            "answers" : [ 
                {
                    "q" : 1.0,
                    "a" : 7.0
                }, 
                {
                    "q" : 1.0,
                    "a" : 5.0
                }, 
                {
                    "q" : 2.0,
                    "a" : 9.0
                }
            ]
        }
    ]
}

I want to remove all item in answers which q equal 1.0, following is expect document:

{
    "_id" : ObjectId("58760d8caa30c585ef8d3beb"),
    "results" : [ 
        {
            "item" : "A",
            "score" : 5.0,
            "answers" : [ 
                {
                    "q" : 2.0,
                    "a" : 6.0
                }
            ]
        }, 
        {
            "item" : "B",
            "score" : 8.0,
            "answers" : [ 
                {
                    "q" : 2.0,
                    "a" : 9.0
                }
            ]
        }
    ]
}

I used:

db.getCollection('test').update({"results.answers.q":1},
                                { $pull: {"results.$.answers": {q:1} } },
                                { multi: true })

But got:

{
    "_id" : ObjectId("58760d8caa30c585ef8d3beb"),
    "results" : [ 
        {
            "item" : "A",
            "score" : 5.0,
            "answers" : [ 
                {
                    "q" : 2.0,
                    "a" : 6.0
                }
            ]
        }, 
        {
            "item" : "B",
            "score" : 8.0,
            "answers" : [ 
                {
                    "q" : 1.0,
                    "a" : 7.0
                }, 
                {
                    "q" : 1.0,
                    "a" : 5.0
                }, 
                {
                    "q" : 2.0,
                    "a" : 9.0
                }
            ]
        }
    ]
}

in item B there still have embed documents which q equal 1

How can I do?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Unfortunately, there is no way to achieve this result with a single query, unless someone can think of something really smart with $where operator. Usage of positional operator implies that you can only target a single array element (in your case it is a single results element), and this usage is a must in any subarray pulls.

An ugly workaround here is to perform this update N times where N is the length of results array. This approach will do the job, but obviously it will be a huge performance issue if these arrays are large.

over 4 years ago · Santiago Trujillo Relatório

0

using the below query will work for you. For more info regarding $pull please visit https://docs.mongodb.com/manual/reference/operator/update/pull/

db.getCollection('test').update({},{ $pull: {"results":{"answers": {$elemMatch:{q:1} }} }},{ multi: true });
over 4 years ago · Santiago Trujillo Relatório

0

You can now achieve this with $pull, as per the MongoDB docs: https://docs.mongodb.com/manual/reference/operator/update/pull/#pull-array-of-documents

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