eded
eded

Reputation: 3958

mongodb mapreduce groupby twice

I am new to mongodb and try to count how many distinct login users per day from existing collection. The data in collection looks like following

[{
    _id: xxxxxx,
    properties: {
        uuid: '4b5b5c2e208811e3b5a722000a97015e',
        time: ISODate("2014-12-13T00:00:00Z"),
        type: 'login'
    }
}]

Due to my limited knowledge, what I figure out so far is group by day first, output the data to a tmp collection and use this tmp collection to do anther map reduce and output the result to a final collection. This solution will get my collections bigger which I do not really like it. Does anyone can help me out or any good/more complex tutorials that I can follow? thanks

Upvotes: 0

Views: 237

Answers (1)

Verran
Verran

Reputation: 4082

Rather than a map reduce, I would suggest an Aggregation. You can think of an aggregation as somewhat like a linux pipe, in that you can pass the results of one operation to the next. With this strategy, you can perform 2 consecutive groups and never have to write anything to the database.

Take a look at this question for more details on the specifics.

Upvotes: 1

Related Questions