На данный момент у меня есть 2 таблицы: 1) основной материал, другой - 2) таблица прогноза.localField - Материал, ForeignField - МАТЕРИАЛ.Для одного материала есть несколько записей в таблице прогнозов для каждого месяца.Теперь мне нужна месячная, годовая и текущая квартальная сумма из таблицы прогнозов для материала.
Но я не могу получить несколько сумм (год, месяц).
db.material_master.aggregate(
[
{
"$lookup" : {
"from" : "monthly_forcast_table",
"localField" : "Material",
"foreignField" : "MATERIAL",
"as" : "forcast"
}
},
{
"$unwind" : {
"path" : "$forcast",
"preserveNullAndEmptyArrays" : true
}
},
{
"$match" : {
"forcast.EMPLOYEE CODE" : "47000006"
}
},
{
"$group" : {
"_id" : {
"$cond" : [
{
"Financial Year" : "2016-2107"
},
{
"$sum" : "$forcast.Qty"
},
0.0
]
}
}
}
],
{
"allowDiskUse" : true
}
);
Данные
основная запись материала
{
"_id" : ObjectId("5affd515415668486654f95e"),
"Focus Code" : "RMTEC0049",
"Material" : "100000000",
"Material type" : "ZROH",
"Brand code" : "1003",
"Brand name" : "Raw Material",
"Old Material Number" : "RMTEC0049",
"Material description" : "Tech Hexaconazole"
}
{
"_id" : ObjectId("5affd515415668486654f95f"),
"Focus Code" : "RMEMF0103-2",
"Material" : "100000001",
"Material type" : "ZROH",
"Brand code" : "1002",
"Brand name" : "Raw Material",
"Old Material Number" : "RMEMF0103-2",
"Material description" : "MEGASOLVE ESBO"
}
и forcust
{
"_id" : ObjectId("5affaa3e6f8be823dea6f1b0"),
"PRICE" : 0.0,
"STATE" : "GJ",
"REGION" : "GJ-NS",
"DEPOT" : "AHMEDABAD",
"EMPLOYEE CODE" : "47000006",
"EMPLOYEE NAME" : "GJ Jignesh",
"CATEGORY" : "NUCLEUS",
"BRAND" : "BRAND",
"PRODUCT NAME" : "BRAND ( L ) - 1 Ltr",
"PRODUCT CODE" : "FGMNL1041",
"MATERIAL" : "810000088",
"Financial Year" : "2015-2016",
"Month" : "2015-04-01",
"Qty" : 0.0
}
{
"_id" : ObjectId("5affaa3e6f8be823dea6f1b1"),
"PRICE" : 0.0,
"STATE" : "GJ",
"REGION" : "GJ-NS",
"DEPOT" : "AHMEDABAD",
"EMPLOYEE CODE" : "47000006",
"EMPLOYEE NAME" : "GJ Jignesh",
"CATEGORY" : "NUCLEUS",
"BRAND" : "BRAND",
"PRODUCT NAME" : "BRAND ( L ) -500 ML",
"PRODUCT CODE" : "FGMNL0007",
"MATERIAL" : "810000095",
"Financial Year" : "2015-2016",
"Month" : "2015-04-01",
"Qty" : 0.0
}
{
"_id" : ObjectId("5affaa3e6f8be823dea6f1b2"),
"PRICE" : 15.0,
"STATE" : "GJ",
"REGION" : "GJ-NS",
"DEPOT" : "AHMEDABAD",
"EMPLOYEE CODE" : "47000006",
"EMPLOYEE NAME" : "GJ Jignesh",
"CATEGORY" : "NUCLEUS",
"BRAND" : "BRAND",
"PRODUCT NAME" : "BRAND ( L ) -250 ML",
"PRODUCT CODE" : "FGMNL0008",
"MATERIAL" : "810000094",
"Financial Year" : "2015-2016",
"Month" : "2015-04-01",
"Qty" : 15.0
}
Ожидаемый результат:
Yearly sum: Qty sum for "Financial Year" : "2015-2016"
last 3 month sum : "Month" : "2015-04-01","2015-05-01","2015-06-01"
last 1 month sum : "Month" : "2015-06-01"