Nosql模型结构 [英] Nosql model structure

查看:59
本文介绍了Nosql模型结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何构建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)

推荐答案

我需要查询游戏"集合,并找到选定团队是主队还是客队的所有游戏.

I need to query the Games collection and find all games where selected team is either home team or away team.

关于这种查询,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:

Cloud Firestore不支持以下类型的查询:

Cloud Firestore does not support the following types of queries:

  • 逻辑或查询.在这种情况下,您应该为每个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:

OR查询,因为那些查询将需要跳过索引,这将无法保证性能.注意,正在进行中的工作是支持可能的IN查询的子集,以便您可以查询给我所有提供泰国或意大利美食的餐厅"之类的信息.

OR queries, since those would require skipping through the index, which would make it impossible to guarantee the performance. Note that work is under way to support a subset of possible IN queries, so that you could query for something like "give me all restaurants that serve either Thai or Italian food".

您可以等待此功能可用,或者可以使用上面的我的解决方案.

You can wait till this feature will be available or you can use my solution above.

我应该在那儿通过Firestore生成的ID来引用团队吗?还是我应该输入团队名称?

Should i have references to the teams by there firestore generated id or should i go for team names?

在大多数情况下,我们通常使用用户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模型结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆