'Difference between aggregate and find with map mongodb
I tried to run this code to know the response time between aggregate and a simple find with .map() javascript function .
console.time("time 1")
Models.Student.aggregate([
{
$lookup:
{
from: "schools",
localField: "School",
foreignField: "_id",
as: "school"
}
},{$match:{'school.name':"my favourite school"}}]).then(students=>{
console.timeEnd("time 1")
})
console.time("time 2")
Models.Student.find().populate({path:'School',match:{name:"my favourite school"}}).then(students=>{
students=students.filter(student=>student.School)
console.timeEnd("time 2")
})
And i got this result of time for both codes :
time 2: 24.131s
time 1: 44.981s
Can someone tell me please why would we use aggregate when it needs twice of the needed time by the simple find and map.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
