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
See Question&Answers more detail:
os