Can't get Map/Reduce to work with MongoDB <p>I've got this code:</p> <pre><code>// construct map and reduce functions $map = new MongoCode(" function() { ". "var key = {date: this.timestamp.getFullYear()+this.timestamp.getMonth()+this.timestamp.getDate()};". "emit(key, {count: 1}); }" ); $reduce = new MongoCode(" function(k, vals) { ". "var sum = 0;". "for (var i in vals) {". "sum += vals[i];". "}". "return sum;". "}" ); $dates = $db-&gt;command(array( "mapreduce" =&gt; "items", "map" =&gt; $map, "reduce" =&gt; $reduce, //"query" =&gt; array("type" =&gt; "sale"), "out" =&gt; array("merge" =&gt; "distinct_dates") ) ); </code></pre> <p>But get this error when testing: </p> <blockquote> <p>Array ( [assertion] => map invoke failed: JS Error: TypeError: this.timestamp.getFullYear is not a function nofile_b:0 [assertionCode] => 9014 [errmsg] => db assertion failure [ok] => 0 ) Cron run finished.</p> </blockquote> <p>Each object in the collection has a timestamp, from which I want to extract a date (Ymd), and put each distinct date in a new collection.</p>