Hello,
I know that
source_collection.aggregate([{
'$lookup': {
localField: '_id',
from: foreign_collection,
foreignField: 'foreign_id',
as: 'joined',
},
}, {
'$unwind': {
path: '$joined',
},
}])
Will utilize the following index if it is available:
foreign_collection.index({
foreign_id: 1,
})
However, I don’t see it mentioned anywhere if
source_collection.aggregate([{
'$lookup': {
localField: '_id',
from: foreign_collection,
foreignField: 'foreign_id',
pipeline: [{
'$match': {
date => { '$gt': cut_off_date },
},
}],
as: 'joined',
},
}, {
'$unwind': {
path: '$joined',
},
}])
will use
foreign_collection.index({
foreign_id: 1,
date: 1,
})
or not.
The use case is pretty common if you ask me; it is equivalent to joining with some custom filter conditions in SQL.
explain
won’t help here as it doesn’t mention anything related to indexes.
How do I make sure that $lookup with an inner $match utilizes the index?
And if it doesn’t, can I request the feature now? How?
P.S. The code in this post is all pseudocode.