MongoDB:使用嵌套的$ group进行聚合

时间:2018-08-25 22:36:21

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我有以下查询:

 db.test.aggregate([
    {
        $match: {
            'type': 'energy'
        }
    },
    {
        $limit: 10000
    },
    {
        $addFields: {
            day: {
                $dateToString: {
                    date: "$when.date",
                    format: "%d/%m/%Y"
                }
            },
            sensor: "$id"
        }, 
    }, 
    {
        $project: {
            _id: 1,
            sensor: 1,
            when: 1,
            value: 1,
            day: 1
        }
    },    
    {
        $group: {
            _id: "$day",
            data: {
                $push: "$$ROOT"
            },

        }
    },
    {
        $sort: {
            'data': 1
        }
    }
])

哪个返回以下数据格式:

 {
    "_id" : "05/04/2018",
    "data" : [
        {
            "_id" : ObjectId("5ac66be9b02d5c18fd4106c7"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-05T13:53:22.703-03:00"),
                "unix" : 1522947202,
                "milli" : 1522947202703
            },
            "day" : "05/04/2018",
            "sensor" : "sen3"
        },
        {
            "_id" : ObjectId("5ac66be9b02d5c18fd4106c8"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-05T13:53:22.705-03:00"),
                "unix" : 1522947202,
                "milli" : 1522947202705
            },
            "day" : "05/04/2018",
            "sensor" : "sen4"
        }
    ]
},
{
    "_id" : "06/04/2018",
    "data" : [
        {
            "_id" : ObjectId("5ac7e5d2efe88a4e76c008d2"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-06T18:25:38.885-03:00"),
                "unix" : 1523049938,
                "milli" : 1523049938885
            },
            "day" : "06/04/2018",
            "sensor" : "sen3"
        },
        {
            "_id" : ObjectId("5ac7e5e4efe88a4e76c008d5"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-06T18:25:56.105-03:00"),
                "unix" : 1523049956,
                "milli" : 1523049956105
            },
            "day" : "06/04/2018",
            "sensor" : "sen3"
       }
    ]
},

...

请注意,每个“数据”文档中都有不同类型的传感器(sen3,sen4,...,senN)。

我正在尝试再次汇总此结果,按传感器对数据进行分组,以得到如下所示的输出:

  {
    "_id" : "05/04/2018",
    "sen3" : [
        {
            "_id" : ObjectId("5ac66be9b02d5c18fd4106c7"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-05T13:53:22.703-03:00"),
                "unix" : 1522947202,
                "milli" : 1522947202703
            },
            "day" : "05/04/2018",
            "sensor" : "sen3"
        },
        {
            "_id" : ObjectId("5ac66be9b02d5c18fd4106c7"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-05T13:53:22.703-03:00"),
                "unix" : 1522947202,
                "milli" : 1522947202703
            },
            "day" : "05/04/2018",
            "sensor" : "sen3"
        }
    ],
    "sen4" : [
        {
            "_id" : ObjectId("5ac66be9b02d5c18fd4106c8"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-05T13:53:22.705-03:00"),
                "unix" : 1522947202,
                "milli" : 1522947202705
            },
            "day" : "05/04/2018",
            "sensor" : "sen4"
        },
        {
            "_id" : ObjectId("5ac66be9b02d5c18fd4106c8"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-05T13:53:22.705-03:00"),
                "unix" : 1522947202,
                "milli" : 1522947202705
            },
            "day" : "05/04/2018",
            "sensor" : "sen4"
        }
    ]
},
{
    "_id" : "06/04/2018",
    "sen3" : [
        {
            "_id" : ObjectId("5ac7e5d2efe88a4e76c008d2"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-06T18:25:38.885-03:00"),
                "unix" : 1523049938,
                "milli" : 1523049938885
            },
            "day" : "06/04/2018",
            "sensor" : "sen3"
        },
        {
            "_id" : ObjectId("5ac7e5e4efe88a4e76c008d5"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-06T18:25:56.105-03:00"),
                "unix" : 1523049956,
                "milli" : 1523049956105
            },
            "day" : "06/04/2018",
            "sensor" : "sen3"
        }
    ],
    "sen4": [
        {
            "_id" : ObjectId("5ac7e7a7efe88a4e76c008de"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-06T18:33:27.365-03:00"),
                "unix" : 1523050407,
                "milli" : 1523050407365
            },
            "day" : "06/04/2018",
            "sensor" : "sen4"
        },
        {
            "_id" : ObjectId("5ac7e7a7efe88a4e76c008de"),
            "value" : 0,
            "when" : {
                "date" : ISODate("2018-04-06T18:33:27.365-03:00"),
                "unix" : 1523050407,
                "milli" : 1523050407365
            },
            "day" : "06/04/2018",
            "sensor" : "sen4"
        }
    ]
}

简而言之:我想将数据按传感器分组,并在每个传感器内部进行分组属于那一天和传感器。

我正在尝试创建嵌套的$group,但在所有尝试中,它都出错了。

这是可行的吗,是的,怎么办?

1 个答案:

答案 0 :(得分:2)

您可以在mongodb 3.6 及更高版本

中尝试以下聚合
db.collection.aggregate([
  { "$match": { "type": "energy" }},
  { "$limit": 10000 },
  { "$addFields": {
    "day": { "$dateToString": { "date": "$when.date", "format": "%d/%m/%Y" }},
    "sensor": "$id"
  }}, 
  { "$project": { "_id": 1, "sensor": 1, "when": 1, "value": 1, "day": 1 }},
  { "$group": {
    "_id": { "day": "$day", "sensor": "$sensor" },
    "data": { "$push": "$$ROOT" }
  }},
  { "$group": {
    "_id": { "day": "$_id.day" },
    "data": { "$push": { "k": "$_id.sensor", "v": "$data" }}
  }},
  { "$addFields": { "data": { "$arrayToObject": "$data" }}},
  { "$replaceRoot": { "newRoot": { "$mergeObjects": [ "$_id", "$data" ] }}}
])