I’m seeing two ‘strange’ behaviours when using $lookup.
- Index not being used when the local field is a dot path
- Index not being used unless there’s a $limit stage beforehand
Both cases use the same two collections:
survey (has 3,000 documents):
{ attributes.personId: 123 }
person (has 250,000 documents):
{ attributes.personId: 123, attributes.name: "Marvin" }
attributes.personId has a unique index on it.
1. Index not being used when local field is a dot path
When running the following pipeline on survey, the unique index isn’t used:
[
{
$lookup: {
from: "person",
localField: "attributes.personId",
foreignField: "attributes.personId",
as: "person"
}
}
]
But if I do this, it is (sort of, see point 2):
[
{
$project: {
personId: "$attributes.personId"
}
},
{
$lookup: {
from: "person",
localField: "personId",
foreignField: "attributes.personId",
as: "person"
}
}
]
2. Index not being used unless there’s a $limit stage beforehand
Even with the $project the index isn’t used unless I put a $limit stage beforehand:
[
{
$project: {
personId: "$attributes.personId"
}
},
{ $limit: 50000 },
{
$lookup: {
from: "person",
localField: "personId",
foreignField: "attributes.personId",
as: "person"
}
}
]
Now, the unique index is used and the query flies along.
The index isn’t used with localField: "attributes.personId" even with the $limit stage.
Is this all expected behaviour?!
(Note: I want to use this query periodically to get names into the surveys so I’m not using lookups all the time when querying)