MongoDB嵌套GroupBy

时间:2018-10-15 14:16:57

标签: mongodb aggregation-framework

我有一个示例数据,如下所示: (每个楼层都有多个传感器数据)

{"Floor_Id": "Galileo_001",
    "name": "Forklifts",
    "Sensor_data": [{
            "Floor_Id": "Galileo_001",
            "Floor_name": "Forklifts",
            "Name": "forkLift_002",
            "Asset_Id": 123,
            "Load": 1.7096133,
            "Timestamp": 1537878750996
        },
        {
            "Floor_Id": "Galileo_001",
            "Floor_name": "Forklifts",
            "Name": "forkLift_003",
            "Asset_Id": 456,
            "Load": 1.7096133,
            "Timestamp": 1537878750996,
        },
        {
            "Floor_Id": "Galileo_001",
            "Floor_name": "Forklifts",
            "Name": "forkLift_005 ",
            "Asset_Id": 127,
            "Load": 1.7096133,
            "Timestamp": 1537878750996
        },
        {
            "Floor_Id": "Galileo_001",
            "Floor_name": "Forklifts",
            "Name": "forkLift_001",
            "Asset_Id": 157,
            "Load": 1.7096133,
            "Timestamp": 1537878750996,
        }
]}

对于响应,我需要计算每个楼层的总负载和每天的单个负载。所需的响应如下:

{
    "TotalLoad": 3214,
    "Floor_Id": "Galileo_001",
    "LoadUnit": "Kgs",
    "AssetStatus": [{
        "TotalLoad": 200,
        "LoadUnit": "Kgs",
        "Date": "1539588994"
    }, {
        "TotalLoad": 400,
        "LoadUnit": "Kgs",
        "Date": "1539475200"
    }, {
        "TotalLoad": 100,
        "LoadUnit": "Kgs",
        "Date": "1539388800"
    }]
}

我正在编写以下Mongo汇总:

db.sensordata.aggregate([{"$unwind" : "$Sensor_data" },
{"$group": {
     "_id": {"Floor_Id": "$Floor_Id", 
         "DailyDate":{"$dateFromParts":{
                    "year":{"$year":{"$add": [new Date("1970-01-01"), "$Sensor_data.Timestamp"]}},
                    "month":{"$month":{"$add": [new Date("1970-01-01"), "$Sensor_data.Timestamp"]}},
                    "day":{"$dayOfMonth":{"$add": [new Date("1970-01-01"), "$Sensor_data.Timestamp"]}}
                      } 
                  }
             },
     "AssetLoad": {"$sum": "$Sensor_data.Load" }
    }
},
{
    "$group" : {
            "_id": {"Floor_Id": "$_id.Floor_Id"},
            "TotalLoad": { "$sum": "$Sensor_data.Load" },
            "AssetStatus":{
                "$push":{
                    "TotalLoad": "$AssetLoad",
                    "LoadUnit": "Kgs",
                    "Date": "$_id.DailyDate"
                    }
                }
        }
}
])

问题: 对于每个楼层计算出的总负载,我得到0。

{
    "_id" : {
        "Floor_Id" : "Galileo_001"
    },
    "TotalLoad" : 0,
    "AssetStatus" : [ 
        {
            "TotalLoad" : 8.5480665,
            "LoadUnit" : "Kgs",
            "Date" : ISODate("2018-09-25T00:00:00.000Z")
        }
    ]
}

我在这里做错了什么? 如何获得所需的输出?

1 个答案:

答案 0 :(得分:1)

由于第二个$group流水线无法识别字段Sensor_data.Load,因此默认值为0,因此您将获得零。

替换表达式

"TotalLoad": { "$sum": "$Sensor_data.Load" },

管道更改的字段为AssetLoad

"TotalLoad": { "$sum": "$AssetLoad" },

要计算每个楼层共有的传感器数量,您的第一个管道需要计算传感器的大小。 使用 $size 传感器数据,使用 $addFields 将其存储在新字段中,然后使用 $first operator.audit

将管道修改为以下内容:

db.sensordata.aggregate([
    { "$addFields": {
      "TotalSensors": { "$size": "Sensor_data" }
    } },
    {"$unwind" : "$Sensor_data" },
    { "$group": {
      "_id": {
          "Floor_Id": "$Floor_Id", 
          "DailyDate": {
              "$dateFromParts": {
                  "year": { "$year": {
                      "$add": [new Date("1970-01-01"), "$Sensor_data.Timestamp"]
                  } },
                  "month": { "$month": {
                      "$add": [new Date("1970-01-01"), "$Sensor_data.Timestamp"]
                  } },
                  "day": { "$dayOfMonth": {
                      "$add": [new Date("1970-01-01"), "$Sensor_data.Timestamp"]
                  } }
              } 
          }
      },
      "AssetLoad": {"$sum": "$Sensor_data.Load" },
      "TotalSensors": { "$first": "$TotalSensors" }
    } },
    { "$group" : {
        "_id": {"Floor_Id": "$_id.Floor_Id"},
        "TotalLoad": { "$sum": "$Sensor_data.Load" },
        "AssetStatus":{
            "$push": {
              "TotalLoad": "$AssetLoad",
              "LoadUnit": "Kgs",
              "Date": "$_id.DailyDate"
            }
        },
        "TotalSensors": { "$first": "$TotalSensors" }
    } }
])