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

248
Vistas
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 Respuestas
Responde la pregunta

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 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