开发者

Datastore fetch on two filters alternative?

I have a datastore entity called Game and two fields in it called playerOne and playerTwo. Either of these fields stores a username.

开发者_运维百科

I need to search on the Game entity and return a MAX of 30 games where the username can be either playerOne OR playerTwo...

So in a relational database you would go: SELECT * FROM Game WHERE playerOne='username' OR playerTwo='username' LIMIT 30

But in big table you can't filter on more than one field! I can't fetch 10 from one and 10 from the other as the number from each can be variable and in createdDate order.

How would you do this in your datastore?


The quick answer is create a StringListProperty that contains [player_a, player_b] and then simply use the multi-value index made out of that:

games = Game.all().filter("players =", player_find)


You can not do an OR query on the datastore using different fields. If you have to keep your current entity model then you have to do two queries.
1) filtering on playerOne and limiting to 30
2) filtering on playerTwo and limiting to (30 - result size of query one)
Then merge the results in memory to produce the final set of 30.
Now if you also want some ordering by date, then it will get more tricky. However the SQL query you wrote doesn't have any ordering so I omitted it aswell.

However if you can change the entity model then a good way to achive what you want is to have a single field containing a list of both usernames.
Then you can do a simple query in the style of:
SELECT * FROM Game WHERE playerBoth = 'username'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜