복붙노트

[MONGODB] MongoDB를 - 월별로 그룹

MONGODB

MongoDB를 - 월별로 그룹

나는 MongoDB의에서 쿼리를 쓰고 있어요. 나는 예약 테이블을하고 난 달 즉 그룹 달에 예약의 수를 싶어. 나는 일로부터 한 달 얻는 방법 것을 혼동하고있다.

여기 내 스키마는 다음과 같습니다

{
    "_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 }

어디에서 잘못을 하겠어? 친절하게 도와주세요.

해결법

  1. ==============================

    1.당신은 당신이 아니라 당신이 $ 달 연산자를 사용할 수없는 날짜의 문자열로 bookingdatetime를 저장하고 있으므로 있기 때문에, 당신의 집계 파이프 라인에서 임의의 자바 스크립트를 포함 할 수 없습니다.

    당신은 당신이 아니라 당신이 $ 달 연산자를 사용할 수없는 날짜의 문자열로 bookingdatetime를 저장하고 있으므로 있기 때문에, 당신의 집계 파이프 라인에서 임의의 자바 스크립트를 포함 할 수 없습니다.

    날짜 문자열은 엄격한 형식을 따라야하기 때문에, 당신은 문자열에서 월 값을 추출하기 위해 $의 SUBSTR 연산자를 사용할 수 있습니다 :

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

    출력 :

    {
        "result" : [ 
            {
                "_id" : "03",
                "numberofbookings" : 1
            }, 
            {
                "_id" : "07",
                "numberofbookings" : 1
            }, 
            {
                "_id" : "10",
                "numberofbookings" : 1
            }
        ],
        "ok" : 1
    }
    
  2. ==============================

    2.당신은 당신의 그룹에 $ 월 키워드를 사용해야합니다. 새 날짜 (). 속하는 getMonth ()를 호출 한 번만 일어날 것, 그리고 시도하고 문자열 "$ bookingdatetime"에서 한 달을 만듭니다.

    당신은 당신의 그룹에 $ 월 키워드를 사용해야합니다. 새 날짜 (). 속하는 getMonth ()를 호출 한 번만 일어날 것, 그리고 시도하고 문자열 "$ bookingdatetime"에서 한 달을 만듭니다.

    db.booking.aggregate([
        {$group: {
            _id: {$month: "$bookingdatetime"}, 
            numberofbookings: {$sum: 1} 
        }}
    ]);
    
  3. from https://stackoverflow.com/questions/27366209/mongodb-group-by-month by cc-by-sa and MIT license