title: Populate last element of an array in Mongoose
body: <p>I have a collection with an array field (<code>&quot;array&quot;</code>) that stores _ids which reference another collection. I am using mongoose's <code>.populate</code> and can populate a specific array element using the string dot notation, e.g.</p>
  <p><code>.populate({path: &quot;array.4&quot;})</code></p>
  <p>but I would now like to populate the last element of the array. In an ideal world, <code>&quot;array.-1&quot;</code> would work but it does not. I have tried using populate's <code>match</code> property with something like:</p>
  <pre><code>.populate({
    path: &quot;array&quot;,
    match: { 
      $arrayElemAt: { 
        $subtract: [
          { 
            $size: &quot;array&quot;
          },
          1
        ]
      }
    },
  })
  </code></pre>
  <p>but this doesn't work either (and I don't think is how it's supposed to be used at all!)</p>
  <p>Is there any way to achieve this?</p>