Hello,
I am having an aggregation use case where I have a large JSON containing product orders data that I need to aggregate.
Below is a small subset of the data which I am planning to aggregate and seek your assistance.
[
{
"orderid": "1111",
"order_type": "individual",
"users": [
{
"name": "user1",
"phone": 982928,
"items": [
{
"name": "AAA",
"qty": 20,
"price": 10
},
{
"name": "BBB",
"qty": 30,
"price": 15
}
]
},
{
"name": "user2",
"phone": 948783,
"items": [
{
"name": "AAA",
"qty": 10,
"price": 10
},
{
"name": "CCC",
"qty": 5,
"price": 20
}
]
},
{
"name": "user3",
"phone": 787868,
"items": [
{
"name": "BBB",
"qty": 40,
"price": 10
},
{
"name": "CCC",
"qty": 15,
"price": 20
}
]
}
]
},
{
"orderid": "2222",
"order_type": "bulk",
"users": [
{
"name": "user1",
"phone": 982928
},
{
"name": "user3",
"phone": 787868
}
],
"items": [
{
"name": "AAA",
"qty": 3,
"price": 10
},
{
"name": "BBB",
"qty": 15,
"price": 10
}
]
}
]
The output I am looking to achieve would look something like:
[
{
"user":"user1",
"orders":[1111,2222],
"unique_items":2
},
{
"user":"user2",
"orders":[1111],
"unique_items":2
},
{
"user":"user3",
"orders":[1111,2222],
"unique_items":3
}
]
Here is a link to mongo playground for convenience.
I am relatively new at aggregation, any help in this regard would be highly appreciated.