How do I find a max value in datastore?


April 2019


705 time


I am using go and datastore and I want to have a query where I get the max value from the datastore. I can't find anything about max values in the docs. How can I accomplish that?

I am using Google App Engine

1 answers


There's no direct max function, but you can do a your_query.Order('-the_field').Limit(1). Minus is for descending sorting.

This will sort the entries by the_field and give you the top result, which has the maximum value.

Note that if you have several entries that share the maximum value, you'll receive only one of them. This is easy to amend with a larger Limit.