title: MongoDB Mapreduce: query/restriction/filter on result of mapreduce?
body: <p>In querying a mongoDB database with map_reduce, I need to limit the actual results. I was hoping to use the <code>query</code> option but this only restricts data in the <code>map</code> part. I need to restrict output.</p>
  
  <p>My data looks like this (left other attributes out):</p>
  
  <pre><code> obj1: { name: "obj1", version: 1, someAttr = "a" }, 
   obj1a: { name: "obj1", version: 2, someAttr= "b"},
  
   obj2: { name: "obj2", version: 1 }, 
   obj2a: { name: "obj2", version: 2} ...
  </code></pre>
  
  <p>I have objects who share the same <code>name</code> per version (which is incremented per update).</p>
  
  <p>I need to get the <strong>newest version</strong> of each object, and I have written map/reduce functions (it's done in mongomapper):</p>
  
  <p><strong>MAP:</strong></p>
  
  <pre><code>function() {
    emit(this.name, this);
  }
  </code></pre>
  
  <p><strong>REDUCE:</strong></p>
  
  <pre><code>function(key, values) {
    var res = values[0];
    for(var i=1; i&lt;values.length; i++)
    {
      if(values[i].version &gt;res.version)
      {
        res = values[i];
      }
    }
    return res;
  }
  </code></pre>
  
  <p>Now from time to time I need to restrict results and I juse the <code>query</code> option for that. IT works fine, except in one use case:</p>
  
  <p><strong>I need the latest version of obj1 that has also <code>someAttr</code> set to <code>"a"</code>.</strong></p>
  
  <p>So what I tried in <code>query</code> is:</p>
  
  <pre><code>name: "obj1", someAttr: "a"
  </code></pre>
  
  <p>The <strong>expected outcome</strong> is an <strong>empty result</strong> because the latest version does not have <code>someAttr</code> set to <code>"a"</code>. What I got is obj1 with version 1...</p>
  
  <p>How can I restrict the <strong>output</strong> to only those objects with <code>someAttr</code> set to <code>"a"</code>?</p>