title: Mongo: Sum Value in a Field with map/reduce
body: <p>I have a collection that looks like so:</p>
  
  <pre><code>{
      "_id" : ObjectId("foo-1"),
      "status" : "good",
      "value" : "100.00"
  },
  
  {
      "_id" : ObjectId("foo-2"),
      "status" : "good",
      "value" : "100.00"
  }
  </code></pre>
  
  <p>I'd like to get two things:</p>
  
  <ul>
  <li>Total "value" of all objects</li>
  <li>Total "value of all objects with particular status</li>
  </ul>
  
  <p>Here are my map/reduce functions:</p>
  
  <pre><code>var map = function () {
      emit("total", {sum: this.value )) 
      }
  
  var reduce = function (key, values) {
      var result = {sum:0};
      values.forEach(function (value) {result.sum += value.value;});
      return result;
      }
  
  var t = db.leads.mapReduce(map, reduce, {out : "total"});
  db[t.result].find(); 
  </code></pre>
  
  <p>The output of the above is:</p>
  
  <pre><code>PRIMARY&gt; db[t.result].find();
  { "_id" : "total", "value" : { "sum" : NaN } }
  </code></pre>
  
  <p>Why is "sum" coming back NaN?</p>