0

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

eded
  • 3,778
  • 8
  • 28
  • 42

1 Answers1

1

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.

Community
  • 1
  • 1
Verran
  • 3,942
  • 2
  • 14
  • 22
  • yes, Aggregation pipe line will solve the problem. however, my case is a bit tricky which I did not mention in my post. I need to handle timezone in my mapreduce functions as well, therefore I think I have to use mapreduce instead of aggregation pipeline – eded Aug 19 '14 at 19:10
  • Yeah, that makes it a bit more difficult. Depending on how you're storing timezone, you might be able to do some fancy stuff with the `$project` operator to convert time zones. See the second answer in http://stackoverflow.com/questions/18287493/how-to-deal-with-the-timezone-issue-when-storing-dates-in-utc-using-mongod for more details. – Verran Aug 19 '14 at 19:27