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

105
Vistas
Mongo Aggregate JS 'every' equivalent

I'm using an aggregate and I'd like to add a Boolean property per Document for if all properties in a subArray on the Document are true.

In my example below...CONFLICTS is a subArray of Docs that each contain a Boolean property called Resolved. I just want to add a new allResolved field for if all Conflicts are resolved.

So..in my addFields stage, I have something like

 {
   '$project': {
      'allResolved': {
        '$allElementsTrue': '$CONFLICTS.resolved'
      }, 
    }
  }

so JS equivalent is const allResolved = CONFLICTS.every(conflict => conflict.resolved)

But this seems to always return true which is not correct

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Query

  • this does extra checks
  • check to be array
  • to be not empty array
  • and each member to be true (missing and nulls will be false)

Test code here

aggregate(
[{"$set":
  {"allResolved":
   {"$and":
    [{"$isArray":["$conflicts"]},
     {"$not":[{"$eq":["$conflicts", []]}]},
     {"$reduce":
      {"input":"$conflicts",
       "initialValue":true,
       "in":
       {"$and":[{"$eq":["$$this.Resolved", true]}, "$$value"]}}}]}}}])
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