首页 > 解决方案 > 如何为音乐应用创建 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 名称查询数据库并获取它存在的所有商店?

标签: androidfirebasegoogle-cloud-firestore

解决方案


更新:截至 2019 年 5 月,Cloud Firestore 现在支持集合组查询

使用集合组查询,您可以查询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}

要么,要么您可以等待集合组查询变得可用(虽然目前尚无时间表),然后查看是否允许当前数据结构上的用例。


推荐阅读