I need a little help. I’ve a collection which has a field dimension and that field contains array of different objects, which looks like this
Collection – 1:
[
{
"_id": {
"$oid": "6423cddbf4db0d397b22390a"
},
"dimension": [
{
"_id": {
"$oid": "6423cde4f4db0d397b22390b"
},
"dimensionChild": "Wedding"
},
{
"_id": {
"$oid": "6423ce0af4db0d397b22390c"
},
"dimensionChild": "Business Casual"
},
{
"_id": {
"$oid": "6423ce18f4db0d397b22390d"
},
"dimensionChild": "Formal"
}
]
},
......
]
and in other object where I’ve available ids from above document (collection – 1) in form of array, which looks likes this,
Collection – 2:
[
{
"productTags": [
{
"$oid": "6423ce0af4db0d397b22390c" // id from above doc
},
{
"$oid": "6423b723f226c91a55d756bc" // id from different document
}
]
}
]
Now I need some data like below,
[
{
"_id": {
"$oid": "6423ce0af4db0d397b22390c"
},
"dimensionChild": "Business Casual"
},
{
"_id": {
"$oid": "6423b723f226c91a55d756bc"
},
"dimensionChild": "Business Casual 2"
}
]
Can anyone pls help me to do this. I was using aggregation pipeline to fetch that and my attempt was something like below,
await MODEL.aggregate([
.....,
{'$lookup': { "from": 'TEAGET_COLLECTION', "localField": 'productTags', "foreignField": 'dimension._id', as: 'dimensions' } },
.....,
])
and this actually returning me whole collection 1’s dimension and also those records whose id was not present in Collection – 2’s productTags.
Can anyone please help me to know more about this query.
Thanks,
Soham Roy
2
Answers
Collection 1 joins with collection 2. The output for this stage will be the document with a
dimensions
array.As
$lookup
stage in MongoDB is LEFT JOIN. To perform join, you have to filter the document withdimensions
is not an empty array.Set the
productTags
field by concatenating nested array fromdimensions.productTags
into an array via$reduce
.Filter the
dimension
array with the document that its_id
is existed in theproductTags
array.$unwind
– Deconstruct thedimension
array into multiple documents.$replaceWith
– Replace the input document withdimension
object.Demo @ Mongo Playground
Another option is:
See how it works on the playground example