title: mongoDB aggregate nested document to only show three at a time
body: <p>Here is my table:</p>
  <pre><code>    {
          &quot;_id&quot; : ObjectId(&quot;5f2e8e0d4976ed2c04fc2e90&quot;),
          &quot;reference&quot; : &quot;5f2e8e0d4976ed2c04fc2e8f&quot;,
          &quot;id&quot; : &quot;5e87187f6e8c15766e6994f8&quot;,
          &quot;name&quot; : &quot;no name&quot;,
          &quot;items&quot; : [
                  {
                          &quot;_id&quot; : ObjectId(&quot;5f2e994336caf138c13560fc&quot;),
                          &quot;requestedItems&quot; : &quot;5e87187f6e8c15766e6994f8&quot;,
                          &quot;name&quot; : &quot;no name&quot;,
                  },
                  {
                          &quot;_id&quot; : ObjectId(&quot;5f2e994336caf138c135601c&quot;),
                          &quot;requestedItems&quot; : &quot;5e87187f6e8c15766e6994f8&quot;,
                          &quot;name&quot; : &quot;no name&quot;,
                  },
                  {
                          &quot;_id&quot; : ObjectId(&quot;5f2e994336caf138c135201c&quot;),
                          &quot;requestedItems&quot; : &quot;5e87187f6e8c15766e6994f8&quot;,
                          &quot;name&quot; : &quot;no name&quot;,
                  },
                  {
                          &quot;_id&quot; : ObjectId(&quot;5f2e994336caf138c139521c&quot;),
                          &quot;requestedItems&quot; : &quot;5e87187f6e8c15766e6994f8&quot;,
                          &quot;name&quot; : &quot;no name&quot;,
                  },
                  {
                          &quot;_id&quot; : ObjectId(&quot;5f2e994336caf138c139504f&quot;),
                          &quot;requestedItems&quot; : &quot;5e87187f6e8c15766e6994f8&quot;,
                          &quot;name&quot; : &quot;no name&quot;,
                  },
  
          ],
          &quot;createdAt&quot; : ISODate(&quot;2020-08-08T11:35:41.675Z&quot;),
          &quot;updatedAt&quot; : ISODate(&quot;2020-08-18T13:34:43.077Z&quot;),
          &quot;date&quot; : &quot;8-8-2020&quot;,
          &quot;__v&quot; : 0
  }
  </code></pre>
  <p>I am looking to query only the last three sub-documents in &quot;ITEMS&quot; I have been trying MongoDB aggregate framework but had no success. Here is what I have come up with. Any help out there would be very much appreciated.</p>
  <pre><code>    reqInventory.aggregate([
   {$match:{&quot;id&quot; : &quot;5e87187f6e8c15766e6994f8&quot;}},
    { $addFields: {
      items: {
        $map: {
          input: '$items',
          as: 'new',
          in: {
            requestedItems : '$$new.requestedItems',
            name : '$$new.name',
            items: { $slice: [ '$$new.items', 3 ] }
            
          }
        }
      }
    }}
  </code></pre>