title: MongoDB: Get most recent objects from embedded arrays and append parent field
body: <p>I have an array of companies where each company has an array of subscription objects. A subscription object has an array of items and a date when those items was added. I want to retrieve only the most recently added subscription object from each company. I also need to be able to identify where each subscription object came from, so I need to append the company name somehow. Sorry for the confusing semantics!</p>
  
  <p><strong>The array of companies:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>[
    {
      "_id": "company1",
      "name": "Company 1",
      "subscriptions": [
        {
          "items": [
            {
              "_id": "5e13b0207b05e37f12f05beb",
              "data": 40,
              "price": 39
            },
            {
              "_id": "5e13b0207b05e37f12f05bea",
              "data": 100,
              "price": 59
            }
          ],
          "_id": "5e13b0207b05e37f12f05be9",
          "date_added": "2019-12-24T22:09:36.442Z"
        }
      ]
    },
    { "_id": "company2", "name": "Company 2", "subscriptions": [] },
    {
      "_id": "company3",
      "name": "Company 3",
      "subscriptions": [
        {
          "items": [
            {
              "_id": "5e13a47d7c370667c7e67d7a",
              "data": 40,
              "price": 69
            },
            {
              "_id": "5e13a47d7c370667c7e67d79",
              "data": 20,
              "price": 39
            }
          ],
          "_id": "5e13a47d7c370667c7e67d78",
          "date_added": "2019-12-24T21:19:57.804Z"
        },
        {
          "items": [
            {
              "_id": "5e13a4f87c370667c7e67d7d",
              "data": 35,
              "price": 39
            },
            {
              "_id": "5e13a4f87c370667c7e67d7c",
              "data": 60,
              "price": 59
            }
          ],
          "_id": "5e13a4f87c370667c7e67d7b",
          "date_added": "2020-01-04T21:22:00.832Z"
        }
      ]
    }
  ]</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Desired output:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>[
    {
      "name": "Company 1",
      "items": [
        {
          "_id": "5e13b0207b05e37f12f05beb",
          "data": 40,
          "price": 39
        },
        {
          "_id": "5e13b0207b05e37f12f05bea",
          "data": 100,
          "price": 59
        }
      ],
      "_id": "5e13b0207b05e37f12f05be9",
      "date_added": "2019-12-24T22:09:36.442Z"
    },
    {
      "name": "Company 3",
      "items": [
        {
          "_id": "5e13a4f87c370667c7e67d7d",
          "data": 35,
          "price": 39
        },
        {
          "_id": "5e13a4f87c370667c7e67d7c",
          "data": 60,
          "price": 59
        }
      ],
      "_id": "5e13a4f87c370667c7e67d7b",
      "date_added": "2020-01-04T21:22:00.832Z"
    }
  ]</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>What I am trying:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>await Companies.aggregate([
    { $unwind: '$subscriptions' },
    { $sort: { 'subscriptions.date_added': -1 } },
    { $group: { _id: '$_id' } }
  ])</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Output:</strong>
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>[{"_id":"company1"},{"_id":"company3"}]</code></pre>
  </div>
  </div>
  </p>
  
  <p>I think I filtered out the correct objects (the most recent ones) but I am only retrieving the _id of the root element, not the nested subscription objects. These id's might be used to identify the subscriptions, but I need also need the actual subscription items.</p>