title: How to access nested array of objects in mongodb aggregation pipeline?
body: <p>I have a document like this(this is the result after few pipeline stages)</p>
  <pre><code>[
    {
      &quot;_id&quot;: ObjectId(&quot;5e9d5785e4c8343bb2b455cc&quot;),
      &quot;name&quot;: &quot;Jenny Adams&quot;,
      &quot;report&quot;: [
        { &quot;category&quot;:&quot;Beauty&quot;, &quot;status&quot;:&quot;submitted&quot;, &quot;submitted_on&quot;: [{&quot;_id&quot;: &quot;xyz&quot;, &quot;timestamp&quot;:&quot;2022-02-23T06:10:05.832+00:00&quot;}, {&quot;_id&quot;: &quot;abc&quot;, &quot;timestamp&quot;:&quot;2021-03-23T06:10:05.832+00:00&quot;}] },
        { &quot;category&quot;:&quot;Kitchen&quot;, &quot;status&quot;:&quot;submitted&quot;, &quot;submitted_on&quot;: [{&quot;_id&quot;: &quot;mnp&quot;, &quot;timestamp&quot;:&quot;2022-05-08T06:10:06.432+00:00&quot;}] }
      ]
    },
    {
      &quot;_id&quot;: ObjectId(&quot;5e9d5785e4c8343bb2b455db&quot;),
      &quot;name&quot;: &quot;Mathew Smith&quot;,
      &quot;report&quot;: [
        { &quot;category&quot;:&quot;Household&quot;, &quot;status&quot;:&quot;submitted&quot;, &quot;submitted_on&quot;: [{&quot;_id&quot;: &quot;123&quot;, &quot;timestamp&quot;:&quot;2022-02-23T06:10:05.832+00:00&quot;}, {&quot;_id&quot;: &quot;345&quot;, &quot;timestamp&quot;:&quot;2021-03-23T06:10:05.832+00:00&quot;}] },
        { &quot;category&quot;:&quot;Garden&quot;, &quot;status&quot;:&quot;submitted&quot;, &quot;submitted_on&quot;: [{&quot;_id&quot;: &quot;567&quot;, &quot;timestamp&quot;:&quot;2022-05-08T06:10:06.432+00:00&quot;}] },
        { &quot;category&quot;:&quot;BakingNeeds&quot;, &quot;status&quot;:&quot;submitted&quot;, &quot;submitted_on&quot;: [{&quot;_id&quot;: &quot;891&quot;, &quot;timestamp&quot;:&quot;2022-05-08T06:10:06.432+00:00&quot;}] }
      ]
    }
  ]
  </code></pre>
  <p>I  have user input for time period -</p>
  <pre><code>from - 2021-02-23T06:10:05.832+00:00
  to - 2022-02-23T06:10:05.832+00:00
  </code></pre>
  <p>Now I wanted to filter the objects from the report which lie in a certain range of time, I want to  only keep the object if the <code>&quot;submitted_on[-1][&quot;timestamp&quot;]&quot;</code> is in range of <em>from</em> and <em>to</em> date timestamp.
  I am struggling with accessing the timestamp because of the nesting
  I tried this</p>
  <pre><code>$project: {
    &quot;name&quot;: 1,
    &quot;report&quot;: {
      &quot;category&quot;: 1,
      &quot;status&quot;: 1,
      &quot;submitted_on&quot;: 1,
      &quot;timestamp&quot;: {
        $arrayElemAt: [&quot;$report.cataloger_submitted_on&quot;, -1]
      }
    }
  }
  </code></pre>
  <p>But this gets the last object of the report array <code>{&quot;_id&quot;: &quot;bcd&quot;, &quot;timestamp&quot;:&quot;2022-05-08T06:10:06.432+00:00&quot;}</code> for all the items inside the report. How can I do this to select the last timestamp of each obj.</p>