Sometimes, we want to use populate and aggregate in same statement with MongoDB and Node.js.
In this article, we’ll look at how to use populate and aggregate in same statement with MongoDB and Node.js.
How to use populate and aggregate in same statement with MongoDB and Node.js?
To use populate and aggregate in same statement with MongoDB and Node.js, we can call populate
with the aggregation result returned from aggergate
.
For instance, we write
const pop = async () => {
const appointments = await Appointments.aggregate([
//...
]);
await Patients.populate(appointments, {
path: "patient"
});
return appointments;
}
to call aggregate
to return a promise with the aggregation result.
Then we call populate
with the appointments
aggregation to populate the aggregation result with the 'patient'
field.
And then we return the appointments
result with the populate
results.
Conclusion
To use populate and aggregate in same statement with MongoDB and Node.js, we can call populate
with the aggregation result returned from aggergate
.