title: PyMongo aggregate by taking most recent value of field
body: <p>I would like to group my documents and for certain fields take the value of the record with the most recent timestamp (i.e. most recently inserted/updated value). In the example below, I want to group by user ID and phone, and take the email of the record with the most recent timestamp in that group. My initial strategy is to sort by descending timestamp and take the first value for an aggregation like so:</p>
  
  <pre><code>import pymongo
  ...
  pipeline = [
          {
              "$sort": {"timestamp": -1 }
          },
          { "$group": {
              "_id": {
                      "userId": "$userId",
                      "userPhone": "$userPhone",
                      "userEmail": { "$first" : "$userEmail"},
                      "count": {"$sum": 1}
              }
          }
      ]
  </code></pre>
  
  <p>However I run into the following error:</p>
  
  <pre><code>pymongo.errors.OperationFailure: Unrecognized expression '$first'
  </code></pre>
  
  <p>Is there an equivalent $first function available for pymongo?</p>