Mongo DB find all records with highest value depending on a key field

感情迁移 提交于 2019-12-11 03:12:52

问题


I would like to get for each user in my collection the comment of the record with the highest value.

//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"},
]

Would give the following result:

//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"},
]

With MySQL I would use some join logic

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

With Mongo however this logic does not seem to exist.

I tried to split the code in two by getting first the max values for each user:

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

With this I'm scratching my head for a good way to use the find function and especially the good use of $in to get only the results which match both the values present in my_list


回答1:


It does, but the approach is slightly different:

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



回答2:


You can $sort your documents by value before applying $group with $first

db.myCol.aggregate([
    { 
        $sort: { value: -1 }
    },
    {
        $group: {
            _id: "$user",
            value: { $first: "$value" },
            comment: { $first: "$comment" },
            date: { $first: "$date" }
        }
    },
    {
        $sort: { _id: 1 }
    },
    {
        $project: {
            user: "$_id",
            _id: 0,
            value: 1,
            comment: 1,
            date: 1
        }
    }
])



回答3:


You can even do with two stages only

db.collection.aggregate([
  { "$group": {
    "_id": "$user",
    "doc": {
      "$max": {
        "value": "$value",
        "user": "$user",
        "comment": "$comment",
        "date": "$date"
      }
    }
  }},
  { "$replaceRoot": { "newRoot": "$doc" }}
])

$max always takes the maximum value of the first expression which is passed inside the object. Here it is value.



来源:https://stackoverflow.com/questions/54712993/mongo-db-find-all-records-with-highest-value-depending-on-a-key-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!