title: MongoDB Map Reduce - Finalize to skip some results
body: <p>I have a map reduce function that works on a collection as follows: </p>
  
  <pre><code>function Map() {
      emit (
          this.name, 
          { 
              count : 1,
              flag : this.flag
  
          }
      );
  }
  function Reduce(key, values) {
      var count = 0;
      var flag = false;
  
      for (var i in values){
          count = count + 1;
          if (i.flag)
               flag = true;
      }
  
      var reduced = {
          count : count,
          flag  : flag
      }
      return reduced;
  }
  function Finalize(key, reduced) {
  
      if (reduced.count&gt;10 || reduced.flag){
          var finalized = {
              "count" : reduced.count 
          }
          return reduced;
      }
  
      return null;
  }
  </code></pre>
  
  <p>What I am trying to do is that the Finalize will only return objects that pass a certain threshold (e.g. count>10). Currently it is still returning objects and the count is just null.</p>
  
  <p>Any ideas?</p>