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

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")}
       ]


我想在当年的每个月明智地汇总浪费

results: [
          <month1>:{weight: 200},
          <month2>:{weight: 200},
          ......................
          <month12>:{weight: 200}
]


以及当月的明智之举和当周的明智之举,如下所示

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

results: [
          <day1>:{weight: 100},
          ....................
          <day7>:{weight: 100}
         ]


注意:按周和按天的聚合值只是一个虚拟值,我需要这种输出。

最佳答案

对于与查询相关的所有日期时间,您需要从日期字段-manual here中将其值投影到$ project

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

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
        }
    }
])


以下是我的虚拟数据第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
        }
    ]
}


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

   {$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

                }}



输出


{
    "totalWeightWeekly" : 600,
    "year" : 2016,
    "month" : 3,
    "week" : 10
}


欢迎任何意见!

关于mongodb - 如何分别在mongodb中按天,周,月分别在相应的周,月,年分别汇总数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36965707/

10-13 01:01