Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!