Hope you can help I am new to aggregation queries.
I have a nested data structure that I want to group to produce statistical output. I have a set of orders where the order is for a country, product and product code. An order looks like:
db.orders.findOne();
{
"_id" : ObjectId("5efc6db38cb109193e41c4d3"),
"createdDate" : ISODate("2020-06-25T02:06:25.428Z"),
"data" : {
"nested" : {
"country" : "France"
},
"product" : "Product 4",
"latest" : {
"sub" : {
"code" : "Code 3"
}
}
}
}
I have an aggregation query that groups by country, product and code.
db.getCollection('orders').aggregate([
{
$unwind :{
path: "$data.nested.country"
}
},
{
$group: {
_id: { country: "$data.nested.country", product: "$data.product", code: "$data.latest.sub.code" }
}
}
])
This produces output such as:
{ "_id" : { "country" : "Slovenia", "product" : "Product 3", "code" : "Code 7" } }
{ "_id" : { "country" : "Japan", "product" : "Product 1", "code" : "Code 9" } }
{ "_id" : { "country" : "Japan", "product" : "Product 4", "code" : "Code 4" } }
{ "_id" : { "country" : "China", "product" : "Product 1", "code" : "Code 1" } }
{ "_id" : { "country" : "France", "product" : "Product 3", "code" : "Code 4" } }
{ "_id" : { "country" : "Japan", "product" : "Product 4", "code" : "Code 8" } }
{ "_id" : { "country" : "Japan", "product" : "Product 4", "code" : "Code 5" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 4", "code" : "Code 4" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 4", "code" : "Code 7" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 4", "code" : "Code 2" } }
{ "_id" : { "country" : "China", "product" : "Product 4", "code" : "Code 8" } }
{ "_id" : { "country" : "France", "product" : "Product 4", "code" : "Code 4" } }
{ "_id" : { "country" : "Japan", "product" : "Product 2", "code" : "Code 3" } }
{ "_id" : { "country" : "Japan", "product" : "Product 2", "code" : "Code 6" } }
{ "_id" : { "country" : "Japan", "product" : "Product 2", "code" : "Code 3" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 2", "code" : "Code 9" } }
{ "_id" : { "country" : "China", "product" : "Product 2", "code" : "Code 6" } }
I want to group this data by country and then product and then code so, for example, Japan would have a list of products i.e. Product 4, Product 2 inside each there would be a list of codes so “Product 4”: [“Code 8”,“Code 5”,“Code 3”,“Code 6”,“Code 2”] etc. Since an order can be made for a Product with a particular code more than once for a country I need I think a map of codes and the counts for each code.
{ "_id" : { "country": "Japan", products: [{"product":"Product 2","codes":[{"code":"Code 3","count":2},{"code":"Code 6","count":1]}]