title: Map/Reduce gives me NaN or wrong result (due to null)
body: <p>I am yet to ask another Map/Reduce question.</p>
  
  <p>I have a collection "<code>example</code>" which looks like this:</p>
  
  <pre><code>{
  "userid" : "somehash",
  "channel" : "Channel 1"
  }
  </code></pre>
  
  <p>My Map/Reduce functions look like this:</p>
  
  <pre><code>var map = function () {
      emit(this.channel, {user:this.userid, count: 1});
  }
  
  var reduce = function (key, values) {
      var result = {total:0, unique:0};
      var temp = [];
      values.forEach(function (value) {
          result.total += value.count;
  
          if (temp.indexOf(value.user) == -1) {
              temp.push(value.user);
          }
      });
  
      result.unique += temp.length;
  
      return result;
  }
  </code></pre>
  
  <p>Unfortunately, it gives me some really strange results:</p>
  
  <pre><code>{ "_id" : "Channel 1", "value" : { "total" : NaN, "unique" : 47 } }
  { "_id" : "Channel 2", "value" : { "total" : NaN, "unique" : 12 } }
  { "_id" : "Channel 3", "value" : { "total" : 6, "unique" : 6 } }
  </code></pre>
  
  <p>And it seems like <code>value.count</code> resolves to <code>null</code>, it also seems like "Unique" isn't the correct value as well. What I want to do is to count all the values for each channel and also calculate it in such a way that I can see an unique value for each user. Which means, a document in this collection, <code>example</code>, may occur several times. I want to know all times AND unique times.</p>
  
  <p>I followed this guide: <a href="http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction" rel="nofollow">http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction</a> and I don't know why I get <code>null</code> thrown in my face? Very strange, any good ideas on the subject?</p>
  
  <p>Thanks for the advice and better wisdom.</p>