开发者

Querying from GAE datastore using count

At the moment I am getting the top scores for my Androi开发者_如何转开发d application from the datastore as follows:

     Query query = pm.newQuery(PlayerPersistentData.class);
     query.setOrdering("rating desc");
     query.setRange(0, 25);

However, I would also like to order the results by the count of a secondary column:

E.g. In normal SQL it would be something like this:

select PlayerPersistentData.*, count(achievements) as achievements_count
from PlayerPersistentData
order by rating desc, achievements_count desc

Achievements is defined in the datastore as follows

@Persistent
private Set<Integer> achievements;

Is this possible using the GAE datastore? And if so how should I create my query?

Thanks in advance


You can use the method addSort to your Query object like this:

query.addSort("rating", SortDirection.DESCENDING);
query.addSort("achievements", SortDirection.DESCENDING);


You need to denormalize: store the count as a separate field on your entity, and query on that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜