1

I am trying to find the sum of all commission per day but I don't know what should I do and the below is the mongoDB documents:

"operationData" : {
    "topUp" : {
        "topUpId" : "20160520182653940+043000001792098779520",
        "phoneNumber" : "792098779",
        "commission" : 4,
    }
}
}

{
    "_id" : 2,
    "dateTime" : ISODate("2016-06-20T13:56:53.970Z"),
    "operationData" : {
        "topUp" : {
            "topUpId" : "20160520182653940+043000001792098779520",
            "phoneNumber" : "792098779",
            "commission" : 6,
        }
    }
}

{
    "_id" : 3,
    "dateTime" : ISODate("2016-06-20T13:56:53.970Z"),
    "operationData" : {
        "topUp" : {
            "topUpId" : "20160520182653940+0430000017920987345243",
            "phoneNumber" : "792098779",
            "commission" : 5,
        }
    }
}
Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45

1 Answers1

1

you need to use datetime operators ref here

Name Description

  1. $dayOfYear Returns the day of the year for a date as a number between 1 and 366 (leap year).
  2. $dayOfMonth Returns the day of the month for a date as a number between 1 and 31.
  3. $dayOfWeek Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).
  4. $year Returns the year for a date as a number (e.g. 2014).
  5. $month Returns the month for a date as a number between 1 (January) and 12 (December).
  6. $week Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year).
  7. $hour Returns the hour for a date as a number between 0 and 23.
  8. $minute Returns the minute for a date as a number between 0 and 59.
  9. $second Returns the seconds for a date as a number between 0 and 60 (leap seconds).
  10. $millisecond Returns the milliseconds of a date as a number between 0 and 999.
  11. $dateToString Returns the date as a formatted string.

So you could have this snippet

db.operationData.aggregate([{
            $group : {
                _id : {
                    dayOfYear : {
                        $dayOfYear : "$dateTime"
                    },
                    year : {
                        $year : "$dateTime"
                    }
                },
                commission : {
                    $sum : "$operationData.topUp.commission"
                }
            }
        }
    ])
profesor79
  • 9,213
  • 3
  • 31
  • 52