I have a collection with a composite primary key as such
_id: {
a: 'a',
b: 'b'
}
Explaining the following query, reveals that it does a COLLSCAN and the index is not used
db.collection.find({'_id.a': '1', '_id.b': '2'}).explain()
Changing that query to the following, uses the IDHACK successfully.
db.collection.find({_id: {a: '1', b: '2'}}).explain()
The problem is that this doesn't seem to work if used in the pipeline of a $lookup in an aggregation.
This doesn't return any results:
$lookup: {
from: 'collection',
let: {
pid: '1',
sid: '2',
},
pipeline: [
{
$match: {
_id: {
a: '$$pid',
b: '$$sid',
},
},
},
],
as: 'results',
},
while the format that is not using the index returns results as expected:
$lookup: {
from: 'collection',
let: {
pid: '1',
sid: '2',
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
'$_id.a', '$$pid',
],
}, {
$eq: [
'$_id.b', '$$sid',
],
},
],
},
},
},
],
as: 'results',
},
So my question is, how can I modify the lookup pipeline in order to use the index of the composite primary key?