title: MongoDB aggregation filter based on max value
body: <p>Suppose I have a document structure where one of the fields, <code>X</code>, is an array av objects as shown below.</p>
  
  <pre><code>"X" : [ 
      {
        "A" : "abc",
        "B" : 123
      },
      {
        "A" : "wer",
        "B" : 124
      },
      {
        "A" : "fgh",
        "B" : 124
      }
  ]
  </code></pre>
  
  <p>How can I project only the document where field <code>B</code> has the highest values? And if the maximum value is shared by several documents, I just want to return one of them (not important which one). In this case the result could look like:</p>
  
  <pre><code>"X" : [ 
      {
        "A" : "wer",
        "B" : 124
      }
  ]
  </code></pre>