@Sudhesh_Gnanasekaran sure, think in these two collections:
Collection A with the following documents:
[
{_id: 0, b_id: 0, value: 1},
{_id: 1, b_id: 0, value: 2},
{_id: 2, b_id: 1, value: 4}
]
And Collection B with the following documents:
[{_id: 0, b_value: 0},
{_id: 1, b_value: 1},
{_id: 2, b_value: 2}]
If I do this aggregate:
db.getCollection('A').aggregate([
{
$group: {
_id: "$b_id",
sum_value: {$sum: "$value"}
}
},
{
$lookup: {
from: 'B',
localField: '_id',
foreignField: '_id',
as: 'b_data'
}
}
I know that the output documents will look like this:
[
{_id: 0, sum_value: 3, b_data: [{b_value: 0},
{_id: 1, sum_value: 4, b_data: [{b_value: 1}
]
But again, my question is how lookup works internally. In this case, is lookup doing one find (or something similar) for each one of the two ids incoming to the lookup stage?
I’d like to know the details to be aware of them when I prepare my pipelines.
Thank you!