1

I have a route called '/test_cancellation', which is returning all the cancellations by account_mananger, fronter, closer, management_fee, cancellation date. I want to create a route that return the total of management_fee (sum) by year or month.

This is the route that I have right now:

enter image description here

And, this is the json that is returning:

enter image description here

I want to created a route that use the cancellation_date as parameter to return only the year with total of management_fee (Sum). Like this:

enter image description here

Manfred Tijerino
  • 377
  • 1
  • 3
  • 12

1 Answers1

1

All you need to do is group the year, also add a sort to arrange by order:

CacellationKPI.aggregate([
    {
       $group: {
           _id: { $year: "$cancellation_date" },
           count: { $sum: 1 }
       }
    }
    {
       $sort: {
          count: -1
       }
    }
])
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
  • Thank you Tom. But, there is a way to do sum management_fee and being shown by year, month or day. In the code above is counting all the cancellations by year (Which is awesome) by I want to sum management_fee by year – Manfred Tijerino Jul 20 '19 at 20:43