Jan 26, 2023
Let's assume you're using a newer version of mongoose for instance 6.9.0.
Then you can do this:
const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]);
for await (const doc of agg) {
console.log(doc.name);
}
or
const aggregate = Model.aggregate([
{ $project: { a: 1, b: 1 } },
{ $skip: 5 }
]);
Kindly read more here https://mongoosejs.com/docs/api/aggregate.html