MongoDB - 按月分组

时间:2014-12-08 20:21:32

标签: mongodb date

我在mongo db中编写查询。我有一张预订表,我希望在一个月内获得预订数量,即按月分组。我很困惑如何从约会中获得月份。

这是我的架构:

{
    "_id" : ObjectId("5485dd6af4708669af35ffe6"),
    "bookingid" : 1,
    "operatorid" : 1,
    "clientid" : null,
    "callername" : "Sayem Hussain",
    "contactno" : "0205661862",
    "regular" : 0,
    "bookingdetail" : [
        {
            "driverid" : 1,
            "pickupdatetime" : "2011-04-14 11:00:00",
            "from" : "white chapel",
            "to" : "beach",
            "cost" : 500,
            "journeytime" : 60
        },
        {
            "driverid" : 2,
            "pickupdatetime" : "2012-05-14 12:00:00",
            "from" : "walthamstow",
            "to" : "mile end",
            "cost" : 1000,
            "journeytime" : 50
        }
    ],
    "bookingdatetime" : "2012-10-11T07:00:00Z"
}
{
    "_id" : ObjectId("5485dd6af4708669af35ffe7"),
    "bookingid" : 2,
    "operatorid" : 1,
    "clientid" : 1,
    "callername" : null,
    "contactno" : "0205561281",
    "regular" : 1,
    "bookingdetail" : [
        {
            "driverid" : 3,
            "pickupdatetime" : "2012-02-12 09:00:00",
            "from" : "grange park",
            "to" : "queen mary",
            "cost" : 650,
            "journeytime" : 90
        },
        {
            "driverid" : 2,
            "pickupdatetime" : "2012-02-13 06:00:00",
            "from" : "drapers bar",
            "to" : "naveed restaurant",
            "cost" : 1350,
            "journeytime" : 120
        }
    ],
    "bookingdatetime" : "2014-07-26T05:00:00Z"
}
{
    "_id" : ObjectId("5485dd6af4708669af35ffe8"),
    "bookingid" : 3,
    "operatorid" : 2,
    "clientid" : 2,
    "callername" : null,
    "contactno" : "02565138632",
    "regular" : 1,
    "bookingdetail" : [
        {
            "driverid" : 2,
            "pickupdatetime" : "2013-11-23 06:00:00",
            "from" : "hussainabad",
            "to" : "lyari",
            "cost" : 2450,
            "journeytime" : 240
        },
        {
            "driverid" : 1,
            "pickupdatetime" : "2013-11-25 08:00:00",
            "from" : "garden",
            "to" : "defence",
            "cost" : 1800,
            "journeytime" : 30
        }
    ],
    "bookingdatetime" : "2014-03-17T11:00:00Z"
}

这是我尝试过的:

db.booking.aggregate([{$group:{_id:new Date("$bookingdatetime").getMonth(), numberofbookings:{$sum:1}}}])

然后它返回:

{ "_id" : NaN, "numberofbookings" : 3 }

我哪里错了?请帮助我。

3 个答案:

答案 0 :(得分:12)

您需要在群组中使用$ month关键字。您的new Date().getMonth()来电只会发生一次,并会尝试创建一个月的字符串" $ bookingdatetime"。

db.booking.aggregate([
    {$group: {
        _id: {$month: "$bookingdatetime"}, 
        numberofbookings: {$sum: 1} 
    }}
]);

答案 1 :(得分:9)

您不能在汇总管道中包含任意JavaScript,因为您将bookingdatetime存储为string而不是Date,因此无法使用{{1运营商。

但是,因为您的日期字符串遵循严格的格式,您可以使用$substr运算符从字符串中提取月份值:

$month

输出:

db.test.aggregate([
    {$group: {
        _id: {$substr: ['$bookingdatetime', 5, 2]}, 
        numberofbookings: {$sum: 1}
    }}
])

答案 2 :(得分:0)

您可以使用 $toDate 运算符将字符串转换为日期。在某种程度上,我添加到 answer 给出的 Will Shaver。通过采用这种方法,我能够实现您想要的。

db.booking.aggregate([
    {
        $project: {
            month: { $month: { $toDate: "$bookingdatetime" } },
        }
    },
    {
        $group: {
            _id: { month: "$month" },
            numberofbookings: { $sum: 1 }
        }
    }
]);