Mongo db加入2个或更多集合并获得行结果

时间:2018-05-25 08:08:44

标签: mongodb join collections

我现在有2个表,1)材料主要另一个是2)预测表。 localField是Material,foreignField是MATERIAL。对于单一材料,每个月的预测表有多个条目。现在我需要从预测表中获得每月,每年和当前的季度总和。

但无法获得多笔金额(每年,每月)。

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"

0 个答案:

没有答案