Population manual
- La population manual es una técnica que reemplaza el .populate() automático de Mongoose cuando trabajas con arquitecturas multitenant. En lugar de que Mongoose haga las consultas automáticamente, tú las haces manualmente.
js
// 1. Obtienes los datos sin populate
const historyTreatmentPlants = await HistoryTreatmentPlantModel.find({})
.sort({createdAt: -1})
.lean()
// 2. Haces population manual con Promise.all
const populatedHistoryTreatmentPlants = await Promise.all(
historyTreatmentPlants.map(async (history) => {
if (history.treatmentPlantId) {
const treatmentPlant = await TreatmentPlantModel.findById(
history.treatmentPlantId,
{name:1, industry:1, type:1}
).lean();
return {
...history,
treatmentPlantId: treatmentPlant
};
}
return history;
})
);