如何为音乐应用程序创建Firestore模式?

时间:2019-03-21 09:44:09

标签: android firebase google-cloud-firestore

我有一个Cloud Firestore音乐数据库:

ROOT
 |
 +-- shops {collection}
      |
      +-- countryName {document}
             |
             +-- cityName {collection}
                   |
                   +-- shopIdOne {document}
                         |
                         +-- dvds {collection}
                              |
                              +-- dvdIdOne {document}
                              |     |
                              |     +-- name: "Led Zeppelin: Live in Seattle 1977"
                              |
                              +-- dvdIdTwo {document}
                                    |
                                    +-- name: "Queen: Live at AID 1985"

使用这种模式,我可以从现有城市的一家商店获得所有音乐dvd,这很好。问题在于,同一DVD可能在不同城市的至少5家商店中存在。

我知道我无法查询多个集合,所以问题是,如何创建一个架构,以便我可以按DVD名称查询数据库并获取存在它的所有商店?

1 个答案:

答案 0 :(得分:1)

更新:自2019年5月起,Cloud Firestore现在支持collection group queries

使用集合组查询,您可以查询所有商店中的所有dvds个集合:

db.collectionGroup('dvds').where('name', '==', 'Led Zeppelin: Live in Seattle 1977')

原始答案

如果您希望能够找到具有特定DVD的商店,则也必须将该精确信息存储在数据库中。所以:

ROOT
 |
 +-- DVDs {collection}
      |
      +-- dvdIdOne {document}
      |     |
      |     +-- name: "Led Zeppelin: Live in Seattle 1977"
      |      |
      |      +-- shops {collection}
      |            |
      |            +-- shopIdOne {document}
      |
      +-- dvdIdTwo {document}
            |
            +-- name: "Queen: Live at AID 1985"      |
             |
             +-- shops {collection}
                   |
                   +-- shopIdOne {document}

或者,或者您可以等待集合组查询变为可用(尽管目前尚无时间表),然后查看当前数据结构是否允许用例。