title: MongoDB sort documents by array elements
body: <p>I have document structure like : </p>
  
  <pre class="lang-js prettyprint-override"><code>{
      map: 'A',
      points: [
          {
              type: 'type1',
              distanceToSpawn: 110
          },
          {
              type: 'type4',
              distanceToSpawn: 40
          },
          {
             type: 'type6',
             distanceToSpawn: 30
          }
      ]
  },
  {
      map: 'B',
      points: [
          {
              type: 'type1',
              distanceToSpawn: 100
          },
          {
              type: 'type2',
              distanceToSpawn: 60
          },
          {
              type: 'type3',
              distanceToSpawn: 25
          }
      ]
  },
  {
      map: 'C',
      points: [
          {
              type: 'type2',
              distanceToSpawn: 90
          },
          {
              type: 'type3',
              distanceToSpawn: 1
          },
          {
              type: 'type6',
              distanceToSpawn: 76
          }
      ]
  }
  </code></pre>
  
  <p>I want to get all maps having point type <code>type1</code> sorted by the <code>distanceToSpawn</code> in ascending order.</p>
  
  <p>Expected result :  </p>
  
  <pre class="lang-js prettyprint-override"><code>{
      map: 'B',
      points: [
          {
              type: 'type1',
              distanceToSpawn: 100
          }
      ]
  },
  {
      map: 'A',
      points: [
          {
              type: 'type1',
              distanceToSpawn: 110
          }
      ]
  }
  </code></pre>
  
  <p>I've tried something like : </p>
  
  <pre class="lang-js prettyprint-override"><code>db.maps.find({'points.type': {$eq : 'type1'}}, {map: 1, 'points.$':1}).sort({'points.distanceToSpawn': 1}).limit(10)
  </code></pre>
  
  <p>But this thing not sorting maps by ascending order.</p>
  
  <p>Thanks.</p>