问题
On Google App Engine's ndb, I used the following to retrieve all entities and sort them according to their grade:
ranks = Member.query().order(-Member.grade)
Then I would like to know the position of a specific member:
i = 0
for rank in ranks:
if rank.account == 'abc'
position = i
break
i += 1
My question: is there an equivalent ndb operation to find the position of a specific entity? Thanks.
回答1:
I believe it could be done in two steps
Retrieve the entry whose account is 'abc'
target = Member.query(Member.account=='abc').get()
Query for entries with higher grade
n = Member.query(Member.grade>target.grade).count()
Then you get target's rank as n+1
回答2:
It looks like you are trying to find the rank of a member. Take a look at http://code.google.com/p/google-app-engine-ranklist/ which will enable you to find the rank, with a query, without needing to loop through all the scores.
来源:https://stackoverflow.com/questions/14493479/google-app-engine-ndb-find-the-position-of-an-order-query-list