How do I find a max value in datastore?

Refresh

April 2019

Views

705 time

1

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 https://cloud.google.com/appengine/docs/go/datastore/

1 answers

4

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.