Mongo DB 根据关键字段查找具有最高值的所有记录

Seb*_*n D 5 mongodb mongodb-query aggregation-framework

我想为我的收藏中的每个用户获取具有最高价值的记录的评论。

//myCol
[
    {'user' : 1, 'value':20, "comment": "cloudy", "date":"2018-12-01"},
    {'user' : 2, 'value':5, "comment": "sun","date":"2018-12-01"},
    {'user' : 3, 'value':13, "comment": "rain","date":"2018-13-01"},
    {'user' : 4, 'value':13, "comment": "cloudy","date":"2018-13-01"},
    {'user' : 2, 'value':20, "comment": "rain","date":"2018-01-20"},
    {'user' : 1, 'value':2, "comment": "cloudy","date":"2018-02-02"},
]
Run Code Online (Sandbox Code Playgroud)

会给出以下结果:

//Result 
[
    {'user' : 1, 'value':20, "comment": "cloudy", "date":"2018-12-01"},
    {'user' : 2, 'value':20, "comment": "rain","date":"2018-01-20"},
    {'user' : 3, 'value':13, "comment": "rain","date":"2018-13-01"},
    {'user' : 4, 'value':13, "comment": "cloudy","date":"2018-13-01"},
]
Run Code Online (Sandbox Code Playgroud)

对于 MySQL,我会使用一些连接逻辑

SELECT user FROM myTable as t0
LEFT JOIN (SELECT user, max(value) as maxVal from myTable ) t1 on t0.user = t1.user and t0.value = t1.maxVal
WHERE maxVal IS NOT NULL
Run Code Online (Sandbox Code Playgroud)

然而,对于 Mongo,这种逻辑似乎不存在。

我尝试通过首先获取每个用户的最大值将代码分成两部分:

max_list = myCol.aggregate(
   [
     {
       "$group":
         {
           "_id": "$user",
           "maxValue": { "$max": "$value" }
         }
     },
     { 
            "$project" : {
                "user" : "$_id", 
                "maxValue":"$maxValue",
                "_id":0
            }
        }
   ]
)
 ==> [{'user':1, 'maxValue':20}...]
Run Code Online (Sandbox Code Playgroud)

有了这个,我正在绞尽脑汁寻找一种使用该find函数的好方法,特别是很好地使用 来$in仅获得与中存在的值相匹配的结果my_list

Ale*_*lex 2

确实如此,但方法略有不同:

db.myCol.aggregate([
    {$sort: {value:-1}},
    {$group:{
        _id: "$user",
        doc: {$first: "$$ROOT"}
    }},
    {$replaceRoot: {newRoot: "$doc"} }
])
Run Code Online (Sandbox Code Playgroud)