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

157
Views
Subsequent MongoDB aggregate() calls operate on the result of the previous call!? (using NodeJS driver)

Consider the following code:

const db = mongodb.getDb();
            
// Results count
let count = (await db.collection('sec').aggregate(aggregateQuery)
    .group({ _id: null, count: { $sum: 1 } })
    .project({ _id: 0, count: 1 })
    .toArray());
console.dir(count)

// test
let test = await db.collection('sec').aggregate(aggregateQuery).toArray();
console.dir(test)

The aggregate(aggregateQuery) filters the collection ad performs a lookup with another collection. The first query counts all documents resulting from the aggregate, and it outputs [ { count: 2086 } ] as expected.
In my mind, test should contain a bunch of documents filtered from the collection (according to the aggregate operations), but it is just [ { count: 2086 } ], as in the first query!

If in the second query instead of aggregate(aggregateQuery) I put find(), it outputs all the collection's documents, as expected, so I guess there is something I am missing in the aggregate() function...

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The object passed as a parameter inside aggregate (aggregateQuery in this case) is modified by the following functions (group and project), so when it is used again in the second aggregate call, it is different and yields different results.

over 4 years ago · Santiago Trujillo 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!