I have a document called 'InventoryPerDay' that contain inventory for stores for each day:
{
_id: "20131202/store_a",
_metadata: {
date: ISODate("2013-12-02T00:00:00Z"),
store: "store_a"
},
inventory: {
quantity: {
item_44: 1350,
item_32: 1,
item_2: 1,
item_9: 1
}
}
},
{
_id: "20131201/store_a",
_metadata: {
date: ISODate("2013-12-01T00:00:00Z"),
store: "store_a"
},
inventory: {
quantity: {
item_44: 1000,
item_32: 5,
item_2: 10
}
}
}
I need the total quantity of each item in store_a for both of the days. The items in the 'quantity' hash are unknown. You can see that 'item_9' exists for 02/12/2013 but not for 01/12/2013.
How can i sum unknown nested keys in multiple documents using aggregation in mongodb?
The result for the above example should be:
{
store: "store_a",
inventory: {
quantity: {
item_44: 2350,
item_32: 6,
item_2: 11,
item_9: 1
}
}
}