I have 2 collections. The documents looks like as follows. I have removed other properties for easy understanding:
Collection_A
{
"ref_id" : ObjectId("5e9561edf8beb57100dded8f"),
"features" : [
{
"_id" : ObjectId("5e9561edf8beb57100dded91"),
"k" : "foo",
"v" : "bar"
},
{
"_id" : ObjectId("5e9561edf8beb57100dded92"),
"k" : "foo2",
"v" : "bar2"
}
]
}
Collection_B
{
"ref_id" : ObjectId("5e9561edf8beb57100dded8f")
}
Using aggregate I am trying to find all documents in Collection_B where Collection_B.ref_id == Collection_A.ref_id and Collection_A.features == [{k:foo,v:bar},{k:foo2,v:bar2}]
Basically match supplied features array with $Collection_A.features. Aggregate should return document when all supplied features is present in $Collection_A.features.
After trying, this is the closest I have:
let aggregation_queries = [];
aggregation_queries.push({
$lookup: {
from: "Collection_A",
localField: "ref_id",
foreignField: "ref_id",
as: "Collection_A"
}
});
for(let i = 0; i< features.length; i++)
{
aggregation_queries.push({$match: { $expr: { $in : [features[i].k, "$Collection_A.features.k" ]}}});
}
let aggregateResult = Collection_BSchema.aggregate(aggregation_queries);
This only matches features.k but not features.v. I am trying to find a way to match both fetaures.k and features.v, something like $and: [{features[i].k, "$Collection_A.features.k"}, {features[i].v, "$Collection_A.features.v"}]
I have searched and tried a lot of approaches like $match with $all but doesn't seem to work because match doesn't support $all for ex: "$match":{"$expr":{"$all":["$Collection_A.features",features]} which throws an error" Error: Unrecognized expression '$all'MongoError: Unrecognized expression".
Can someone please help with this or provide some guidance?