Hello,
I got a questions on index using lookup with $in operator.
the data structure look like below:
Collection test_1
{
"_id": {
"$oid": "649a8ce117954132f6fbbb07"
},
"type": "123",
"desc": "...",
"from": "A"
},
{
"_id": {
"$oid": "649a8d4c17954132f6fbbb0b"
},
"type": "456",
"desc": "...",
"from": "B"
}...
Collection test_2
{
"_id": {
"$oid": "649a8e0b17954132f6fbbb0e"
},
"name": "xxx",
"ary": [
{
"type": "123",
"from": "B"
},
{
"type": "456",
"from": "B"
}
]
}...
Aggregation plpeline
[{
$lookup: {
from: 'test_1',
'let': {
type: '$ary.type',
from: '$ary.from'
},
pipeline: [{
$match: {
$expr: {
$and: [{
$in: [
'$type',
'$$type'
]
},
{
$in: [
'$from',
'$$from'
]
}
]
}
}
}],
as: 'result'
}
}]
Index created on test_1
{ "type": 1, "from": 1 },
{ "type": 1 },
{ "from": 1 }
For other reason it is not possible to embed the test_1 into test_2.
When performing the lookup, a collection scan is done. Are there anyway to improve the performance on this lookup?
Thanks.