I have collection
in MongoDB
and it has several records in it and I need to add a column by UF and then return the largest value of that sum. For example
UF: 'SP',
Valor: 10
----
UF: 'SP',
Valor: 23.5
----
UF: 'RJ',
Valor: 40
----
UF: 'PR',
Valor: 5
What I need to do is add the values by state, that is:
SP = 33.5
RJ = 40
PR = 5
And return the highest value in case:
40 (que é de RJ)
All this with mongoDB. I'm using PHP, but that does not interfere. The problem is with query
I do not know how to do it correctly. I tried to make a similar one but it did not work, just returned the maximum number of each state:
{
{
'$group' : {
'_id' : '$UF',
'Maximo' : {
$max: {
{'$sum' : '$Valor'}
}
}
}
}
What I need is to return the maximum number of the sum, not all, only the largest value among all sums. This would like to do without having to give foreach
in PHP, so I wanted to do everything in the query already.