title: MongoDB document setup and aggregation
body: <p>I'm pretty new to MongoDB and while preparing data to be consumed I got into Aggregation... what a powerful little thing this database has! I got really excited and started to test some things :) </p>
  
  <p>I'm saving time entries for a companyId and employeeId ... that can have many entries... those are normally sorted by date, but one date can have several entries (multiple registrations in the same day)</p>
  
  <p>I'm trying to come up with a good schema so I could easily get my data exactly how I need and as a newbie, I would rather ask for guidance and check if I'm in the right path</p>
  
  <p>my output should be as </p>
  
  <pre><code>[{
      "company": "474A5D39-C87F-440C-BE99-D441371BF88C",
      "employee": "BA75621E-5D46-4487-8C9F-C0CE0B2A7DE2",
      "name": "Bruno Alexandre":
      "registrations": [{
          "id": 1448364,
          "spanned": false,
          "spannedDay": 0,
          "date": "2019-01-17",
          "timeStart": "09:00:00",
          "timeEnd": "12:00:00",
          "amount": {
              "days": 0.4,
              "hours": 2,
              "km": null,
              "unit": "days and hours",
              "normHours": 5
          },
          "dateDetails": {
              "week": 3,
              "weekDay": 4,
              "weekDayEnglish": "Thursday",
              "holiday": false
          },
          "jobCode": {
              "id": null,
              "isPayroll": true,
              "isFlex": false
          },
          "payroll": {
              "guid": null
          },
          "type": "Sick",
          "subType": "Sick",
          "status": "APP",
          "reason": "IS",
          "group": "LeaveAndAbsence",
          "note": null,
          "createdTimeStamp": "2019-01-17T15:53:55.423Z"
    }, /* more date entries */ ]
  }, /* other employees */ ]
  </code></pre>
  
  <p>what is the best way to add the data into a collection?</p>
  
  <p><strong>Is it more efficient</strong> if I create a document <strong>per company/employee</strong> and add all registration entries inside that document (it could get really big as time passes)... or is it better to have one document <strong>per company/employee/date</strong> and add all daily events in that document instead?</p>
  
  <p>regarding aggregation, I'm still new to all this, but I'm imagining I could simply call</p>
  
  <pre><code>RegistrationsModel.aggregate([
      {
          $match: {
              date: { $gte: new Date('2019-01-01'), $lte: new Date('2019-01-31') },
              company: '474A5D39-C87F-440C-BE99-D441371BF88C'
          }
      },
      {
          $group: {
              _id: '$employee',
              name: { '$first': '$name' }
          }
      },
      {
          // ... get all registrations as an Array ...
      },
      {
          $sort: {
              'registrations.date': -1
          }
      }
  ]);
  </code></pre>
  
  <p>P.S. I'm taken the <a href="https://www.coursera.org/learn/mongodb-aggregation-framework" rel="nofollow noreferrer">Aggregation course</a> to start familiarized with all of it</p>