Basic Map/Reduce

25 views
Skip to first unread message

tmountain

unread,
Jul 18, 2012, 11:03:03 AM7/18/12
to mongod...@googlegroups.com
Hi guys, trying to do a basic map reduce job and running into a snag. I've tried a few basic tests, and it seems like my reduce job is always ignored. Here's an example:

> var m = function() { emit( this.s, {count : 10} ) } // this.s is a session id
> var r = function(key, values) { return {count : 20}; }
> db.tracking.mapReduce(m, r, "foo")
{
    "result" : "foo",
    "timeMillis" : 36,
    "counts" : {
        "input" : 52,
        "emit" : 52,
        "reduce" : 0,
        "output" : 52
    },
    "ok" : 1,
}
> db.foo.find()
{ "_id" : "201207_01202097a09378b506fa529bd946f356", "value" : { "count" : 10 } }
{ "_id" : "201207_01b56eac4ff502bd7da53bfab8a4ea98", "value" : { "count" : 10 } }

As you can see, all I'm trying to do here is change the value of count, but for some reason the original value is being reported.

tmountain

unread,
Jul 18, 2012, 11:17:08 AM7/18/12
to mongod...@googlegroups.com
Doing some more research of my own, it seems map reduce is optimized to avoid processing single document matches. Apparently, the finalize function is preferred here.

A. Jesse Jiryu Davis

unread,
Jul 18, 2012, 11:34:00 AM7/18/12
to mongod...@googlegroups.com
Your reduce function isn't called unless there are multiple documents with the same key -- in your case, unless several documents with the same 's' value are emitted by your map function.
Reply all
Reply to author
Forward
0 new messages