Example of map/reduce with multiple keys to group by?

2,483 views
Skip to first unread message

JoshL

unread,
Apr 1, 2010, 10:16:46 AM4/1/10
to mongodb-user
Can someone post an example of how you would do a map reduce when you
want to group by multiple keys? The docs give a great group by
example for "select a,b,sum(c) csum from coll where active=1 group by
a,b" but none of the map reduce examples I can find take into account
a AND b, just a.

If anyone has a quick example of doing a map reduce with multiple keys
they can post, it would be appreciated.

Eliot Horowitz

unread,
Apr 1, 2010, 12:01:06 PM4/1/10
to mongod...@googlegroups.com
you just emit an object as your key
emit( { a : 1 , b : 1 } , ... )

> --
> 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.
>
>

JoshL

unread,
Apr 1, 2010, 3:29:36 PM4/1/10
to mongodb-user
Tried that. No joy.

Can someone PLEASE just post a complete example like there is for
group by?

Much appreciated.

Eliot Horowitz

unread,
Apr 1, 2010, 3:34:03 PM4/1/10
to mongod...@googlegroups.com
Why didn't that work...

JoshL

unread,
Apr 1, 2010, 3:40:46 PM4/1/10
to mongodb-user
I dont know. I have this group by:

r = function(obj, prev) { prev.csum += parseFloat(obj.value); ccount+
+; }

db.vector.group({key: {site:true, goal:true, tags:true}, cond: {value:
{ $exists : true }}, reduce: r, initial: {csum: 0, ccount: 0}});

and this works fine but craps out at 10,000 unique rows.

I tried to convert this to map reduce based on what you said,

m = function() { emit{site:1, goal:1, tags:1} }

r = function(k,v) { var csum = 0; var ccount = 0; for(var i in v)
{ csum += parseFloat(v[i].value); ccount++ }; return {csum, ccount} }

and it didnt work. As I mentioned I dont have a complete example to
go off to do this so not sure how to proceed.

Eliot Horowitz

unread,
Apr 1, 2010, 4:12:47 PM4/1/10
to mongod...@googlegroups.com
You should read the docs on emit and look at the basic examplex
this is wrong:

> m = function() { emit{site:1, goal:1, tags:1} }
try
> m = function() { emit{site:1, goal:1 } , { tags:1} }

JoshL

unread,
Apr 1, 2010, 4:25:41 PM4/1/10
to mongodb-user
Eliot -

Ive READ the docs multiple times and looked at the basic examples.

This is why im asking someone to give me a BASIC EXAMPLE for a multi-
key group by using map reduce.

Can someone provide this?

On Apr 1, 4:12 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> You should read the docs on emit and look at the basic examplex
> this is wrong:
>
>
>
> > m = function() { emit{site:1, goal:1, tags:1} }
> try
> > m = function() { emit{site:1, goal:1 } , { tags:1} }

Ludovic Levesque

unread,
Apr 1, 2010, 4:35:33 PM4/1/10
to mongod...@googlegroups.com

JoshL

unread,
Apr 1, 2010, 5:59:26 PM4/1/10
to mongodb-user
Now that's helpful. Thank you Ludovic.
Reply all
Reply to author
Forward
0 new messages