英文:
How do I find a max value in datastore?
问题
我正在使用Go和Datastore,并且我想要一个查询,从Datastore中获取最大值。我在文档中找不到关于最大值的任何信息。我该如何实现这个功能?
我正在使用Google App Engine。
英文:
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
得分: 4
没有直接的max
函数,但是你可以使用your_query.Order('-the_field').Limit(1)
来实现。减号表示降序排序。
这将按照the_field
对条目进行排序,并给出最大值的顶部结果。
请注意,如果有多个条目共享最大值,你将只收到其中一个。如果需要,可以通过增加较大的Limit
来解决这个问题。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论