Help with map reduce

18 views
Skip to first unread message

james

unread,
Jul 2, 2011, 6:57:25 PM7/2/11
to mongodb-user
Hi,

I'm using the Python driver with v 1.8.1.

Having trouble getting reduce to work when there's only 1 document to
reduce. No problems at all when there are >1 document to reduce for a
given key.

Here's an example illustrating my problem:

Source collection - "source-mr"
Output collection - "out-mr"

SOURCE COLLECTION
----------------------------------
> db["source-mr"].find()
{ "_id" : ObjectId("4e0f9c948d4528c39aee706c"), "x" : 1 }


MAP REDUCE CODE
----------------------------------
m = Code("function() { "
" emit(this.x, 1);"
"}");


r = Code("function(key, values) {"
" return 2;"
"}");

res = db["source-mr"].map_reduce(m, r, query={}, out="out-mr")

OUTPUT COLLECTION
----------------------------------
> db["out-mr"].find()
{ "_id" : 1, "value" : 1 }


Reduce is not running or else I would see {"value": 2} in the output
collection. If I add another document to my source collection with
the same key ("x": 1), reduce runs properly and I see {"value": 2} in
my output collection.

Thanks in advance for your help.

James


Sam Millman

unread,
Jul 2, 2011, 7:00:03 PM7/2/11
to mongod...@googlegroups.com
Reduce will not run on a single document.

I am not sure why you need reduce to run on a single document since it just designed to combine emitted data really...you finalize function does post-processing stuff.



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Bernie Hackett

unread,
Jul 5, 2011, 6:09:04 PM7/5/11
to mongodb-user
Sam is correct. In your example with only one document there isn't
anything to reduce. If you try your example in the mongo shell you
will get the same result. If you need to do any kind of post-
processing use the finalize parameter.

On Jul 2, 4:00 pm, Sam Millman <sam.mill...@gmail.com> wrote:
> Reduce will not run on a single document.
>
> I am not sure why you need reduce to run on a single document since it just
> designed to combine emitted data really...you finalize function does
> post-processing stuff.
>
Reply all
Reply to author
Forward
0 new messages