Using MongoDB's map/reduce to "group by" two fields

Posted by ibz on Stack Overflow See other posts from Stack Overflow or by ibz
Published on 2010-05-12T06:52:42Z Indexed on 2010/05/12 10:04 UTC
Read the original article Hit count: 328

Filed under:
|

I need something slightly more complex than the examples in the MongoDB docs and I can't seem to be able to wrap my head around it.

Say I have a collection of objects of the form {date: "2010-10-10", type: "EVENT_TYPE_1", user_id: 123, ...}

Now I want to get something similar to a SQL GROUP BY query, grouping over both date and type. That is, I want the number of events of each type in each day. Also, I'd like to make it unique by user_id, ie. if a user has more events in the same day, count it only once.

I'm trying to do this with map/reduce.

I do

db.logs.mapReduce(function() { emit(this.type, 1); }, function(k, vals) { var total = 0; for (var i = 0; i < vals.length; i++) total += vals[i]; return total; }})

which nicely groups by type, but now, how can I group by date at the same time? Seems the key in emit() can't be an array (I thought about doing emit([this.date, this.type], 1)). Also, how can I ensure the per-user uniqueness?

I'm just starting with MongoDB and I'm still having trouble grasping the basic concepts. Also, there is not much documentation available out there. Any help from more experienced users is appreciated. Thanks!

© Stack Overflow or respective owner

Related posts about mongodb

Related posts about mapreduce