如何在mongodb中分别在相应的周、月、年中按天、周、月聚合数据

Cod*_*cPM 2 mongoose mongodb

在我的项目中,我有一个废物收集,如下所示

wastes:[
        {weight: 100, date: ISODate("2016-01-01T10:20:41.417Z")}
        {weight: 100, date: ISODate("2016-01-01T10:20:41.417Z")}
        {weight: 100, date: ISODate("2016-02-01T10:20:41.417Z")}
        {weight: 100, date: ISODate("2016-02-01T10:20:41.417Z")}
        {weight: 100, date: ISODate("2016-03-01T10:20:41.417Z")}
        {weight: 100, date: ISODate("2016-03-01T10:20:41.417Z")}
        ........................................................
        {weight: 100, date: ISODate("2016-12-01T10:20:41.417Z")}
        {weight: 100, date: ISODate("2016-12-01T10:20:41.417Z")}
       ]
Run Code Online (Sandbox Code Playgroud)

我想在今年的每个月明智地汇总浪费,例如

results: [
          <month1>:{weight: 200},
          <month2>:{weight: 200},
          ......................
          <month12>:{weight: 200} 
]
Run Code Online (Sandbox Code Playgroud)

以及当月的周明智和当前周的逐日如下

results:[
        <week1>:{weight: 100},
        .....................
        <week4>:{weight: 100}
        ]

results: [
          <day1>:{weight: 100},
          ....................
          <day7>:{weight: 100}
         ]
Run Code Online (Sandbox Code Playgroud)

注意:每周和每天的聚合值只是我需要那种输出的虚拟值。

pro*_*r79 6

对于与查询相关的所有日期时间,您需要 $project it 从日期字段中取值 -此处手动

在这种情况下,聚合框架会提供帮助 - 请参阅下面的基本查询,其中包含每日和每周的权重聚合,因此您可以将此查询转换为其他时间段或每个时间段使用一个查询:

db.timing.aggregate([{
        $project : {
            year : {
                $year : "$date"
            },
            month : {
                $month : "$date"
            },
            week : {
                $week : "$date"
            },
            day : {
                $dayOfWeek : "$date"
            },
            _id : 1,
            weight : 1
        }
    }, {
        $group : {
            _id : {
                year : "$year",
                month : "$month",
                week : "$week",
                day : "$day"
            },
            totalWeightDaily : {
                $sum : "$weight"
            }
        }
    },
    {
        $group : {

            _id : {
                year : "$_id.year",
                month : "$_id.month",
                week : "$_id.week"
            },
            totalWeightWeekly : {
                $sum : "$totalWeightDaily"
            },
            totalWeightDay : {
                $push : {
                    totalWeightDay : "$totalWeightDaily",
                    dayOfWeek : "$_id.day"
                }
            }
        }
    }, {
        $match : {
            "_id.month" : 3
        }
    }
])
Run Code Online (Sandbox Code Playgroud)

我的虚拟数据第 3 个月的示例结果如下:

{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 10
    },
    "totalWeightWeekly" : 600,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 7
        }, 
        {
            "totalWeightDay" : 400,
            "dayOfWeek" : 6
        }
    ]
}
{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 9
    },
    "totalWeightWeekly" : 1000,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 4
        }, 
        {
            "totalWeightDay" : 600,
            "dayOfWeek" : 3
        }, 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 7
        }
    ]
}
{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 12
    },
    "totalWeightWeekly" : 400,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 7
        }, 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 2
        }
    ]
}
{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 13
    },
    "totalWeightWeekly" : 200,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 3
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

并根据需要形成形状,您可以使用 $project phase

   {$project:{
                _id:0,
                "year" : "$_id.year", //this could be ommited but use $match to avoid sum of other years
                "month" : "$_id.month",  //this could be ommited but use $match to avoid sum of other months
                "week" :"$_id.week",                
                totalWeightWeekly:1

                }}
Run Code Online (Sandbox Code Playgroud)

输出

{
    "totalWeightWeekly" : 600,
    "year" : 2016,
    "month" : 3,
    "week" : 10
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何意见!