dongqiao9015 2013-01-31 19:22
浏览 29

MongoDB查询以获取密钥发生的次数

So I have a MongoDB document that tracks logons to our app. Basic structure appears thusly:

 [_id] => MongoId Object
        (
            [$id] => 50f6da28686ba94b49000003
        )

    [userId] => 50ef542a686ba95971000004
    [action] => login
    [time] => 1358354984

Now- the challenge is this: there are about 20,000 of these entries. I have been challenged to look at the number of times each user logged in (as defined by userId)...so I am looking for a good way to do this. There are a couple of possible approaches that I've seen (in SQL, for example, I might pull down number of logins by grouping by UserID and doing a count on it- something like SELECT userID, count(*) from....group by UserId...and then sub-selecting on that (CASE WHEN or something in the top select). Anyways- wondering if anyone has any suggestions on the best way to do this. Worst case scenario I can limit the result set and do the grouping in memory- but ideally would like to get the full answer directly from Mongo. The other limitation (even after I get past the first set) is that I am looking to do a unique count by date...which will be even tougher!

  • 写回答

3条回答 默认 最新

  • download92000 2013-01-31 19:25
    关注

    You can use MapReduce to group the results by user ID

    http://docs.mongodb.org/manual/applications/map-reduce/#map-reduce-examples

    Or you can use the Group method:

    db.logins.aggregate(
        { $group : {
            _id : "$userId",
            loginsPerUser : { $sum : 1 }
        }}
    );
    

    For MongoDB 20K or even more won't be a problem to walk and combine them so no worries about performance.

    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效