title: How to get the maximum value of documents based on some conditions
body: <p>I have the following simplified MongoDB collection</p>
  <pre><code>{
      &quot;_id&quot;:1,
      &quot;restaurant&quot;:&quot;Gloriuos Food&quot;,
      &quot;cuisine&quot;:&quot;Afghan&quot;,
      &quot;grades&quot;:[
       {   &quot;grade&quot;:&quot;A&quot;,
           &quot;score&quot;:12
       },
       {
           &quot;grade&quot;:&quot;B&quot;,
           &quot;score&quot;:8
       }
      ]
  }
  {
      &quot;_id&quot;:2,
      &quot;restaurant&quot;:&quot;Ariana&quot;,
      &quot;cuisine&quot;:&quot;Afghan&quot;,
      &quot;grades&quot;:[
       {   &quot;grade&quot;:&quot;A&quot;,
           &quot;score&quot;:8
       },
       {
           &quot;grade&quot;:&quot;B&quot;,
           &quot;score&quot;:18
       }
      ]
  }
  {
      &quot;_id&quot;:3, 
      &quot;restaurant&quot;:&quot;Seuda&quot;,
      &quot;cuisine&quot;:&quot;American&quot;,
      &quot;grades&quot;:[
       {   &quot;grade&quot;:&quot;A&quot;,
           &quot;score&quot;:4
       },
      ]
  }
  {
      &quot;_id&quot;:4,
      &quot;restaurant&quot;:&quot;Carvel&quot;,
      &quot;cuisine&quot;:&quot;American&quot;,
      &quot;grades&quot;:[
       {   &quot;grade&quot;:&quot;A&quot;,
           &quot;score&quot;:8
       },
       {
           &quot;grade&quot;:&quot;B&quot;,
           &quot;score&quot;:8
       }
      ]
  }
  
  </code></pre>
  <p>I would like to have a new collection  where the key is the type of cuisine and the value are the name of the restaurant with the maximum score between all the restaurants with the same type of cuisine and the respective score.
  PS: The score is based only in the first value of the grades array.</p>
  <p>See the expected result of the example</p>
  <pre><code>{&quot;_id&quot;:&quot;Afghan&quot;,&quot;value&quot;:[&quot;Gloriuos Food&quot;,12]}
  {&quot;_id&quot;:&quot;American&quot;,&quot;value&quot;:[&quot;Carvel&quot;,8]}
  </code></pre>
  <p>This result is also valid</p>
  <pre><code>{&quot;_id&quot;:&quot;Afghan&quot;,&quot;value&quot;:{&quot;Gloriuos Food&quot;,12}}
  {&quot;_id&quot;:&quot;American&quot;,&quot;value&quot;:{&quot;Carvel&quot;,8}}
  </code></pre>
  <p>I am trying the following code but It doesn't produce the expected result</p>
  <pre><code>var mapFunction = function(){
      emit(this.cuisine,[this.restaurant,this.grades[0].score]);
  };
  var reduceFunction = function(keys,values){
      var ans;
      var maxi=0;
      for(var ele in values){
          if(ele[1]&gt;maxi){
              maxi=ele[1]
              ans=ele
          }
      }
      return ans;
  };
  db.restaurant.mapReduce(mapFunction,reduceFunction,{out:&quot;cuisine&quot;})
  </code></pre>
  <p>The last code produce the following result:</p>
  <pre><code>{ &quot;_id&quot; : &quot;Afghan&quot;, &quot;value&quot; : null }
  { &quot;_id&quot; : &quot;American&quot;, &quot;value&quot; : null }
  </code></pre>
  <p>I also tried other ways, but it doesn't work</p>