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