title: How can I sort by $elemMatch in MongoDB?
body: <p>In my MongoDB database I have a collection of products. Each product contains release information in an array, e.g.</p>
  
  <pre><code>{
    "name" : "foo",
    "release" : [{
        "region" : "GB",
        "active" : "Y",
        "date" : ISODate("2012-03-01T00:00:00Z")
      }, {
        "region" : "US",
        "active" : "Y",
        "date" : ISODate("2012-09-01T00:00:00Z")
      }, {
        "region" : "FR",
        "active" : "N",
        "date" : ISODate("2010-01-01T00:00:00Z")
      }]
  }
  </code></pre>
  
  <p>I want to find all products that are on active release in the GB region, and sort the results by the GB release date.</p>
  
  <p>I tried to do this using:</p>
  
  <pre><code>db.product.find(
    { "release" : { "$elemMatch" : { "region" : "GB", "active" : "Y" } } }
  ).sort({ "release.date" : 1 });
  </code></pre>
  
  <p>This finds the correct products, but does not sort by the release date from the matched element (instead it sorts on the minimum release date from the array). </p>
  
  <p>From what I have read so far, it looks like sorting by properties of the matched element is not possible, is this correct?</p>
  
  <p>Can it be done with the <a href="http://docs.mongodb.org/manual/applications/aggregation/" rel="nofollow">aggregation framework</a>, and if so how?</p>