We have a shop collection:
{
"_id" : ObjectId("xxxxxx"),
"shop" : "Q4",
"batch" : "5647",
}
{
"_id" : ObjectId("xxxxxx"),
"shop" : "Q4",
"batch" : "2314",
}
First step of aggregation as below:
Aggregates.group("$shop", Accumulators.addToSet("batch", "$batch"))
Output:
{ "_id" : "Q4", "batch" : ["5647", "2314"]}
Now - given another collection inventory
as below,we need to find those in "batch"
above using $in
- and add in output
{
"_id" : ObjectId("xxxxxx"),
"bolt" : "5647",
}
{
"_id" : ObjectId("xxxxxx"),
"bolt" : "0001",
}
{
"_id" : ObjectId("xxxxxx"),
"bolt" : "0004",
}
Expected output:
{ "_id" : "Q4", "batch" : ["5647", "2314"],"bolt":["5647"]}
How do we achieve this?