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

247
Visualizações
How to clean an array from empty objects in mongodb?

Afterwards an aggregation in mongodb I get this results:

[
  my_arr:[
    {
      id: ObjectId('618f7ef057c2923be10d1111')
      //other stuff
    },
    {},
    {},
  ]
]

Is there a way to remove the empty object directly in the aggregation? or it is necessary to do it server side? I get that after doing an unwind and a group.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can add a new aggregation stage ( $project, $set or $addFields) with $filter like this:

{
  "$project": {
    "my_arr": {
      "$filter": {
        "input": "$my_arr",
        "as": "a",
        "cond": {
          "$ne": [
            "$$a",
            {}
          ]
        }
      }
    }
  }
}

This filter get only objects into the array where the object itself is not empty (i.e. is not {})

Example here

Also $set example and $addFields example

Also, you can use $$this to refer the object instead of value from as. Example here

about 4 years ago · Juan Pablo Isaza 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