For some reason, the $eq aggregation operator does not seem to like subdocument syntax
Hi Nefiga,
The reason here is that transactions field is an array of subdocuments, not just a subdocument. If you were to apply $unwind stage first to transform into subdocuments, it should work. In aggregation pipeline $eq is a strict equality only, and it does not reach into arrays.
If transactions.color is an array, the $expr you want to use would be $in. For example:
db.parent.aggregate([
{"$lookup": {
from: 'child',
let: {'primaryColor': '$primary'},
pipeline: [
{ $match:
{ $expr:
{ $in: ["$$primaryColor", "$transactions.color"] }
}
}
],
as: 'childLookup'
}
}
])
I’m trying for a performant result that would be utilizing an index on transactions.color
As documented in $expr behaviour, $expr currently (as of v4.0) does not support multikey indexes.
Regards,
Wan.