-
If many documents referring to the same model are involved in a query, does Example: Option 1: const students = await Student.find();
const schoolIds = [...new Set(students.map((s) => s.school.toString()))];
const schoolsMap = await School.find({ _id: { $in: schoolIds } }).transform(/* array of schools to Map<string, School> */);
students.forEach((student) => {
const school = schoolsMap.get(student.school.toString())!;
// ...
}); Option 2: const students = await Student.find().populate('school');
students.forEach((student) => {
const { school } = student;
// ...
}); My question is: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As written, there shouldn't be any difference. |
Beta Was this translation helpful? Give feedback.
As written, there shouldn't be any difference.
Student.find().populate('school')
executes 2 queries: 1 to find all students, and 1 to find all corresponding schools using{ _id: { $in } }
with deduplicated school ids, just like Option 1. Performance difference should be minimal, if any.