-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Execution
-
Fully Compatible
-
ALL
-
200
-
None
-
None
-
None
-
None
-
None
-
None
-
None
When the foreign collection does not have data but there is an index on it, even if the index is not on the join field, JO will run an Indexed Nested Loop Join and will return wrong results.
Example
db.countries.insertMany([
{ "_id": 1, "name": "USA" },
{ "_id": 2, "name": "Canada" },
{ "_id": 3, "name": "France" }
]);
db.cities.createIndex({ "cityName": 1 });
db.countries.aggregate([
{
"$lookup": {
"from": "cities",
"localField": "_id",
"foreignField": "countryId",
"as": "cities"
}
},
{
"$unwind": {
"path": "$cities",
"preserveNullAndEmptyArrays": true
}
}
]);
Expected results
[
{ _id: 1, name: 'USA' },
{ _id: 2, name: 'Canada' },
{ _id: 3, name: 'France' }
]
JO results
db.adminCommand({ setParameter: 1, internalEnableJoinOptimization: true });
[]
Even if we drop the index by calling db.cities.dropIndexes(); It will still execute an Indexed nested loop using the index on _id.
- is related to
-
SERVER-114566 Fall back from join order optimization on unwind with preserveNullAndEmptyArrays = true
-
- Needs Scheduling
-