title: How can I group months and filtered by year in MongoDB using Node.js?
body: <p>I have json that contain the following objects; cancellation_date and management_fee. I want to take out months from my cancellation_date and group them by year, displaying the total of cancellation by month.</p>
  
  <p>Here is the JSON. The only 2 objects to work on are management_fee and cancellation_date (I want to have the total of management_fee lost per month grouped by years). Here is an example with fake data.</p>
  
  <pre><code>{
          "_id": "5d30c4ac5fbf3c250bb6",
          "user": "5d2f594faa064d82c47c",
          "department_director": "Director 1",
          "account_manager": "Account Manager 1",
          "fronter": "Fronter 2",
          "closer": "Closer 1",
          "order_number": "EDVRV20202020202",
          "business_name": "Busines Name 1",
          "business_website": "www.businessname1.com",
          "client_name": "Client 1",
          "client_phone": 9090909090,
          "client_email": "email1@company.com",
          "type_of_service": "PPC Local",
          "management_fee": 599,
          "sales_date": "2017-04-23T00:00:00.000Z",
          "cancellation_date": "2018-04-23T00:00:00.000Z",
          "country": "USA",
          "state": "Florida",
          "client_budget": "Below",
          "daily_budget": 400,
          "monthly_budget": 20,
          "cancellation_reason": "Business Closed",
          "industry_type": "Real State",
          "cancellation_comments": "Just a comment 3",
          "createdAt": "2019-07-18T19:12:44.824Z",
          "updatedAt": "2019-07-18T19:12:44.824Z",
          "__v": 0
      },
  </code></pre>
  
  <p>Here are some code that I've done so far: </p>
  
  <p>[![enter image description here][1]][1]</p>
  
  <pre><code>router.get('/time_series', auth, async (req, res) =&gt; {
  
      try {
  
        const model_CancellationKPI = await CancellationKPI.aggregate(
          [
             {
               $group : {
                  _id : { month: { $month: "$cancellation_date" }, day: { $dayOfMonth: "$cancellation_date" }, year: { $year: "$cancellation_date" } },
                  totalPrice: { $sum: "$management_fee" },
                  averageQuantity: { $avg: "$management_fee" },
                  count: { $sum: 1 }
               }
             }
          ]
       )
  
       res.json(model_CancellationKPI);
  
      } catch (err) {
        console.error(err.message); 
        res.status(500).send('Server Error')
      }
    });
  </code></pre>
  
  <p>And this is the output: </p>
  
  <p>For instance, in the first JSON shows the total of cancellation in the month 7, day 15 and 2019 with 499</p>
  
  <p><a href="https://i.stack.imgur.com/GEBM0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GEBM0.png" alt="enter image description here"></a></p>
  
  <p>But, I want to have an output like this: </p>
  
  <p><a href="https://i.stack.imgur.com/bQiaq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bQiaq.png" alt="enter image description here"></a></p>