dongzhan1948 2014-08-21 11:35
浏览 274
已采纳

MongoDB过滤来自结果的重复用户

I have a mongo collection with activities in this format:

{
  "_id": 1,
  "user": 1,
  "time": 12345,
  "data": ...
}

Now I want to get the 5 latest entries (the whole entry) from this collection but I want only one entry for each user in the case there are more than one activities from a user in the latest activities. I do not want to filter the result after the DB query. I hope there is a mongoDB way to do this on the DB server.

I would like to perform this query with Doctrine MongoDB ODM but I suspect that this is not possible with the provided methods. But a direct mongo query is fine too.

  • 写回答

1条回答 默认 最新

  • dro80463 2014-08-26 15:46
    关注

    You aren't using a date value for your time, so I'm going to assume "latest" means "largest number in the time". Secondly, I'm going to get the top 2 latest entries with at most one per user. The idea is that only the highest value of the time matters for each user, so we just $group by user after sorting on time while projecting the field values from the $first result seen by $group, then take the top 2 entries overall. The example is in the mongo shell.

    > db.user.find()
    { "_id" : 1, "user" : 1, "time" : 12345, "data" : 48 }
    { "_id" : 2, "user" : 1, "time" : 12346, "data" : 32 }
    { "_id" : 3, "user" : 2, "time" : 347, "data" : 2 }
    { "_id" : 4, "user" : 2, "time" : 384, "data" : 99 }
    { "_id" : 5, "user" : 2, "time" : 384, "data" : 66 }
    { "_id" : 6, "user" : 3, "time" : 3384, "data" : 55 }
    { "_id" : 7, "user" : 3, "time" : 33844, "data" : 3 }
    > db.user.aggregate([
        { "$sort" : { "time" : -1 } }, 
        { "$group" : { 
            "_id" : "$user", 
            "time" : { "$first" : "$time" }, 
            "data" : { "$first" : "$data" } 
            } 
        }, 
        { "$sort" : { "time" : -1 } }, 
        { "$limit" : 2 }
    ])
    { "_id" : 3, "time" : 33844, "data" : 3 }
    { "_id" : 1, "time" : 12346, "data" : 32 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀