I have large data (maybe more than 5000000)
I have 2 options :
1 . Aggregate $look up
data
let data = ParentCol.aggregate(
[
{
// Child 1
$lookup:
{
from: 'child1',
localField: '_id',
foreignField: 'child1Id',
as: child1Doc
}
},
{
// Child 2
$lookup:
{
from: 'child2',
localField: '_id',
foreignField: 'child2Id',
as: child2Doc
}
}
// 2 $lookup more child have child more
.........
]
)
2 . Collection.find()
let parentDoc = ParentCol.find(selector).fetch()
let child1Id = [] ,child2Id = []
// Loop push childId
for(let it of parentDoc){
if(it.child1Id) child2Id.push(it.child1Id)
else if(it.child2Id) child2Id.push(it.child2Id)
}
// Find Child
let child1Doc = Child1Col.find({_id:{$in:child1Id}}).fetch()
let child2Doc = Child2Col.find({_id:{$in:child2Id}}).fetch()
// Child have child and loop push Id to find child more
.........
// At the end loop combine array to parent
1
vs 2
, which one is better for performance ?