title: Aggregation error: $arrayElemAt's first argument must be an array, but is object
body: <p>I'm trying to aggregate a collection in mongo using the following pipeline:</p>
  
  <pre><code>  const results = await Price.aggregate([
      { $match: { date: today } },
      { $unwind: '$points' },
      { $match: { 'points.time': { $gte: start, $lte: now } } },
      { $sort: { 'points.time': 1 } },
      { $project: {
        'high': { $max: '$points.price' },
        'low': { $min: '$points.price' },
        'open': { $arrayElemAt: ['$points', 0] },
        'close': { $arrayElemAt: ['$points', -1] }
      } }
    ])
  </code></pre>
  
  <p>However the <code>$arrayElemAt</code> operator isn't working preseumably because one of the preceding stages (<code>$unwind</code> I believe) converts the array of points I have in my documents to an object. How can I fix this?</p>
  
  <p>Example document:</p>
  
  <pre><code>{
      "_id" : ObjectId("5c93ac3ab89045027259a23f"),
      "date" : ISODate("2019-03-21T00:00:00Z"),
      "symbol" : "CC6P",
      "points" : [
          {
              "_id" : ObjectId("5c93ac3ab89045027259a244"),
              "volume" : 553,
              "time" : ISODate("2019-03-21T09:35:34.239Z"),
              "price" : 71
          },
          {
              "_id" : ObjectId("5c93ac3ab89045027259a243"),
              "volume" : 1736,
              "time" : ISODate("2019-03-21T09:57:34.239Z"),
              "price" : 49
          },
        ....
      ],
  </code></pre>
  
  <p>My expected result is an array of objects where the points that should be passed to the project stage should be points in the specified range in the second <code>$match</code>. I tried combining the two <code>$match</code> stages and removing the <code>$unwind</code> stage and the error is gone however the time range isn't being applied</p>