如何通过单个聚合添加两个集合

时间:2018-09-07 05:07:22

标签: mongodb mongoose mongodb-query aggregation-framework

我是MongoDb的新手,希望您对此查询有所帮助。我编写了以下聚合管道。我写了来自collection1的查询,我得到了输出(“常规能源”:0.0036),我写了查询collection2,我得到了输出(“ LastMonthConsumption”:2.08),但是如何通过“(LastMonthConsumption”:2.08”添加两个具有单个聚合的集合常规能源”:0.0036 /常规能源”:0.0036)这是我的要求输出

我在mongodb中有此数据:

COLLECTION 1:DATA
{
    "slcId" : "51",
    "clientId" : "1",
    "dcuId" : "1",
    "type" : "L",
    "officeId" : "200-24",
    "lampStatus" : "OFF",
    "cummulativeKWH" : 133.7,
    "powerFactor" : 1.0,
    "createDate" : ISODate("2018-09-06T00:01:34.816Z")
},
{
    "slcId" : "52",
    "clientId" : "1",
    "dcuId" : "1",
    "type" : "L",
    "officeId" : "200-24",
    "lampStatus" : "OFF",
    "cummulativeKWH" : 133.7,
    "powerFactor" : 1.0,
    "createDate" : ISODate("2018-09-07T21:01:34.816Z")
}
COLLECTION2:DATA
{
    "_class" : "MongoStreetLightMonthlyVo",
    "timeId" : ISODate("2018-08-04T16:40:08.817Z"),
    "vendor" : "CIMCON",
    "slcId" : "123450",
    "mongoStreetLightChildVo" : {
        "totalConsumptionMtd" : 2.08,
        "prevConsumptionMtd" : 3.45,
        "perChargeKWH" : 9.85,

    }
},
{
    "_class" : "MongoStreetLightMonthlyVo",
    "timeId" : ISODate("2018-09-04T16:40:08.817Z"),
    "vendor" : "CIMCON",
    "slcId" : "123450",
    "mongoStreetLightChildVo" : {
        "totalConsumptionMtd" : 2.08,
        "prevConsumptionMtd" : 3.45,
        "perChargeKWH" : 9.85,

    }
}

收藏1:

db.collection1.aggregate([

               { $match:{"type" : "L"}},
                {
                 $count: "TOTAL_Lights"
                },
                { "$project": { 
                  "Conventional Energy": {
                 "$divide": [ 
               { "$multiply": [
               { "$multiply": [ "$TOTAL_Lights" ,0.12 ] },

                 ]},
              1000
              ]
            } 
         }},

       ])

输出:{“常规能源”:0.0036}

Collection2:

    db.collection2.aggregate(
    [
    // Stage 1
    {
        $group: {
         _id:{year:{$year:"$timeId"},month:{$month:"$timeId"} },
         LastMonthConsumption  : {$sum:"$mongoStreetLightChildVo.totalConsumptionMtd"}, 

        }
    },
    {
        $redact: {
          $cond: {  if: { $and:[
            {$eq: [ "$_id.year", {$year:new Date()} ]}, 
            {$eq: [-1, {$subtract:[ "$_id.month", {$month:new Date()} ]}]}
            ]},
                  then: "$$KEEP",
                  else: "$$PRUNE"
          }
        }
    },
    {$project:{
        _id:0,
        LastMonthConsumption :1

    }
}

]
);

输出:{     “ LastMonthConsumption”:2.08 }

预期输出:

LastMonthConsumption - Conventional Energy/Conventional Energy*100

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总

db.collection2.aggregate([
  { "$group": {
    "_id": { "year": { "$year": "$timeId" }, "month": { "$month": "$timeId" }},
    "LastMonthConsumption": { "$sum": "$mongoStreetLightChildVo.totalConsumptionMtd" }
  }},
  { "$redact": {
    "$cond": {
      "if": {
        "$and": [
          { "$eq": ["$_id.year", { "$year": new Date() }] },
          { "$eq": [-1, { "$subtract": ["$_id.month", { "$month": new Date() }] }]
          }
        ]
      },
      "then": "$$KEEP",
      "else": "$$PRUNE"
    }
  }},
  { "$lookup": {
    "from": "collection1",
    "pipeline": [
      { "$match": { "type": "L" } },
      { "$count": "TOTAL_Lights" },
      { "$project": {
        "ConventionalEnergy": {
          "$divide": [{ "$multiply": [{ "$multiply": ["$TOTAL_Lights", 0.12] }] }, 1000]
        }
      }}
    ],
    "as": "ConventionalEnergy"
  }},
  { "$project": {
    "_id": 0,
    "totalConsumption": {
      "$multiply": [
        {
          "$divide": [
            {
              "$subtract": [
                "$LastMonthConsumption",
                { "$arrayElemAt": ["$ConventionalEnergy.ConventionalEnergy", 0] }
              ]
            },
            { "$arrayElemAt": ["$ConventionalEnergy.ConventionalEnergy", 0] }         
          ]
        },
        100
      ]
    }
  }}
])