与文档中的对象列表聚合

时间:2017-02-03 08:56:19

标签: mongodb aggregation-framework spring-data-mongodb

首先,我解释我的情景如下。

我有以下格式的文件。

假设文档名称为“ABC”,文档格式为

{
  "_id" : ObjectId("...."),
  "name" : "....",
  "datalist" : [
    {
        "date" : ISODate("2016-12-01T18:30:00Z"),
        "data" : 20
    },
    {
        "date" : ISODate("2016-12-08T18:30:00Z"),
        "data" : -310
    },
    {
        "date" : ISODate("2016-11-17T18:30:00Z"),
        "data" : 197
    },
    {
        "date" : ISODate("2016-10-06T18:30:00Z"),
        "data" : -126
    },
    {
        "date" : ISODate("2016-07-20T18:30:00Z"),
        "data" : -344
    },
    {
        "date" : ISODate("2016-07-15T18:30:00Z"),
        "data" : -98
    },
    {
        "date" : ISODate("2016-03-01T18:30:00Z"),
        "data" : -254
    },
    {
        "date" : ISODate("2016-02-11T18:30:00Z"),
        "data" : -24
    },
  ]
}

我必须找到“datalist”的每个索引的平均值,从相同日期到3,6和12个月之前的日期列表,并在“datalist”中用相同的对象索引将其放回

所以我的输出应该是

{
  "_id" : ObjectId("...."),
  "datalist" : [
    {
        "date" : ISODate("2016-12-01T18:30:00Z"),
        "data" : 20,
        "threeMonthAvg": -54.75,
        "sixMonthAvg" : -110.167,
        "twelveMothAvg" : -117.375
    },
    {
        "date" : ISODate("2016-12-08T18:30:00Z"),
        "data" : -310,
        "threeMonthAvg": -79.67,
        "sixMonthAvg" : -136.2,
        "twelveMothAvg" : -137
    },
    {
        "date" : ISODate("2016-11-17T18:30:00Z"),
        "data" : 197,
        "threeMonthAvg": 35.5,
        "sixMonthAvg" : -92.75,
        "twelveMothAvg" : -108.167
    },
    {
        "date" : ISODate("2016-10-06T18:30:00Z"),
        "data" : -126,
        "threeMonthAvg": -189.33,
        "sixMonthAvg" : -189.33,
        "twelveMothAvg" : -169.2
    },
    {
        "date" : ISODate("2016-07-20T18:30:00Z"),
        "data" : -344,
        "threeMonthAvg": -221,
        "sixMonthAvg" : -180,
        "twelveMothAvg" : -180
    },
    {
        "date" : ISODate("2016-07-15T18:30:00Z"),
        "data" : -98,
        "threeMonthAvg": -98,
        "sixMonthAvg" : -125.33,
        "twelveMothAvg" : -125.33
    },
    {
        "date" : ISODate("2016-03-01T18:30:00Z"),
        "data" : -254,
        "threeMonthAvg": -139,
        "sixMonthAvg" : -139,
        "twelveMothAvg" : -139
    },
    {
        "date" : ISODate("2016-02-11T18:30:00Z"),
        "data" : -24,
        "threeMonthAvg": -79.6666666667,
        "sixMonthAvg" : -24,
        "twelveMothAvg" : -24
    },
  ]
}

我现在正试着吼叫。

 db.ABC.aggregate([
    {
        $match : { "id" : ...}
    },
    { 
       $unwind : "$datalist"
    },
    {
        $group : {
            _id : "$id",
            datalist : {
                $push :{
                    date : "$datalist.date",
                    data : "$datalist.data",
                    rollingAvgThreeMonth : {
                        $avg : { 
                            $and: [
                                { $gte: [ "$datalist.date", new Date("$datalist.date") ] }, 
                                { $lte: [ "$datalist.date", new Date().setDate( new Date("$datalist.date").getMonth() - 3) ]}
                            ]
                        }                            
                    }
                }   
            }
        }
    }
])

0 个答案:

没有答案