I’m getting started with MongoDB and having trouble merging two nested arrays in different collections.
the first collection.
stores (a sample store)
{
"_id": {
"$oid": "636b8caa92758a885c06798a"
},
"org_id": {
"$oid": "636a064d8137cb5f659b26d9"
},
"name": "store 1",
"location": "merkato",
"type": "store",
"inventory": [
{
"product_id": 6,
"quantity": 10
},
{
"product_id": 5,
"quantity": 3
},
{
"product_id": 7,
"quantity": 9
}
]
}
The second collection
products (shared across multiple stores) there is a shared objectId org_id that will be used to join the documents.
{
"_id": {
"$oid": "636a064d8137cb5f659b26da"
},
"org_id": {
"$oid": "636a064d8137cb5f659b26d9"
},
"product_list": [
{
"product_name": "A30",
"product_id": 5,
"category": "phone",
"model": "SM-7500",
"price": 190.00,
"active": true,
"brand": "Samsung",
"description": null
},
{
"product_name": "A33",
"product_id": 6,
"category": "phone",
"model": "SM-8500",
"price": 270.00,
"active": true,
"brand": "Samsung",
"description": null
},
{
"product_name": "S20",
"product_id": 7,
"category": "phone",
"model": "SM-2500",
"price": 385.00,
"active": true,
"brand": "Samsung",
"description": null
}
],
"product_count": 7
}
The desired output
"name": "store 1",
"location": "merkato",
"inventory": [
{
"product_id": 6,
"product_name": "A33",
"price": 270.00,
"quantity": 10
},
{
"product_id": 5,
"product_name": "A30",
"price": 190.00,
"quantity": 3
},
{
"product_id": 7,
"product_name": "S20",
"price": 385.00,
"quantity": 9
}
]
Thank you in advance.