title: Mongodb select all fields group by one field and sort by another field
body: <p>We have collection 'message' with following fields</p>
  
  <pre><code>_id |   messageId |  chainId | createOn
  
  1   |       1     |    A     | 155
  2   |       2     |    A     | 185
  3   |       3     |    A     | 225
  4   |       4     |    B     | 226
  5   |       5     |    C     | 228
  6   |       6     |    B     | 300
  </code></pre>
  
  <p>We want to select all fields of document with following criteria</p>
  
  <ol>
  <li>distict by field 'chainId'<br></li>
  <li>order(sort) by 'createdOn' in desc order</li>
  </ol>
  
  <p>so, the expected result is</p>
  
  <pre><code>_id |   messageId |  chainId | createOn
  
  3   |       3     |    A     | 225
  5   |       5     |    C     | 228
  6   |       6     |    B     | 300
  </code></pre>
  
  <p>We are using spring-data in our java application. I tried to go with different approaches, nothing helped me so far.<br>
  Is it possible to achieve above with single query?</p>