问题描述
您将如何构建Cloud Firestore数据库.
How would you structure your Cloud Firestore db.
我收藏了团队,竞技场和游戏:
I have collections of Teams, Arenas and Games:
public class Team {
public String name;
public String homeCourtId;
}
public class Game{
public String matchId;
public String date;
public Arena arena;
public Team homeTeam;
public Team awayTeam;
}
public class Arena {
public String name;
public String phone;
public String email;
public String address;
public String postalCode;
public String city;
public String district;
public String cordLat;
public String cordLong;
}
在游戏中,一个团队可以是主队也可以是客队,而游戏始终具有竞技场.
A team can be a home team or away team in Games and a Game always has an Arena.
因此,目前我的想法是将所有比赛都纳入游戏"收藏夹,当我想查找某个团队的所有比赛时,我需要查询游戏"收藏夹并查找所有所选球队是主队或主队的比赛.客队.
So currently my thought is to structure this as all games go into the Games collection and when i want to find out all games for a Team i need to query the Games collection and find all games where selected team is either home team or away team.
我应该通过Firestore生成的ID引用团队吗?还是应该输入团队名称?我能从中获得更多关于此的任何指示吗?
Should I have references to the teams by there Firestore generated id or should I go for team names? Any pointers where i can read more about this?
他们是在Firestore中构建数据的更好方法吗? (或任何nosql db)
Is their a better way to structure my data in Firestore? (or any nosql db)
推荐答案
关于这种查询,Cloud Firestore官方文档非常明确.不幸的是,谈到Firestore,有一些查询限制:
Cloud Firestore official documentation is quite explicit regarding this kind of query. Unfortunately, there are some query limitations, when it comes to Firestore:
- 逻辑或查询.在这种情况下,您应该为每个OR条件创建一个单独的查询,然后将查询结果合并到您的应用中.
因此,您无法查询集合来查找所选团队是一次主队或客队的所有比赛.
So you cannot query a collection to find all games where selected team is either home team or away team in single go.
一种工作是将您的数据库查询两次,并在客户端合并这些查询的结果.这不是完美的,因为您需要查询两次,但我认为它可以解决问题.
A workaroung would be to query your database twice and combine the results of those queries client side. It's not perfect, since you need to query twice but I think it will do the trick.
对于Android,这是您可以在本地合并两个查询的方式:
For Android, this is how you can merge two queries locally:
但是,根据弗兰克·范·普菲伦(Frank van Puffelen)关于同一主题的答案:
But, according to Frank van Puffelen's answer regarding the same topic:
您可以等待此功能可用,或者可以使用上面的我的解决方案.
You can wait till this feature will be available or you can use my solution above.
在大多数情况下,我们通常使用用户ID而不使用名称,原因是名称可以更改,而ID则可以更改.但是,由您决定是否最好使用名称而不是ID进行搜索.
In most of the cases, we asually use user ids and not names, the reason being that names can change while ids not. But it's up to you to decide if it's better to search by names rather than by ids.
P.S.如果您不是在查询"OR"
,而是在查询"AND"
,请注意,允许链接多个whereTo()
调用.
P.S. If you're looking instead of "OR"
query an "AND"
query, please note that chaining multiple whereTo()
calls is permitted.
这篇关于Nosql模型结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!