【问题标题】:Hitting maximum number of composite indexes in Firestore在 Firestore 中达到最大复合索引数
【发布时间】:2019-04-29 05:28:33
【问题描述】:

我有一个 Firestore 数据库,我在其中存储了一个集合scores 中各个级别的游戏级别分数,如下所示:

player1: { levels: {
 1: { score: 2343, timestamp: t1 },
 2: { score: 434, timestamp: t2 },
},
player2: { levels: {
 1: { score: 23, timestamp: t3 },
 2: { score: 34, timestamp: t4 },
}

为了获得任何级别 X 的最高分,我运行查询:

collection('scores')
 .orderBy('levels.X.score')
 .orderBy('levels.X.timestamp', 'desc')

这需要为每个级别创建一个复合索引:collection:score, levels.x.score asc, levels.x.timestamp desc

200 个复合索引的限制意味着我只能在我的数据库中创建 200 个级别。 有没有办法可以增加限制,可能需要付出一些代价? 或者有没有办法可以修改我的数据库结构/查询以防止达到此限制?

【问题讨论】:

    标签: firebase indexing google-cloud-firestore


    【解决方案1】:

    Firestore 限制无法更改。

    您将不得不更改数据建模方式。考虑将每个分数作为单独的文档存储在可以查询的集合中。所以你会:

    /scores             // collection for all scores
      /some-id          // random id for this document with scores
        - playerId
        - level
        - score
        - timestamp
    

    然后您可以查询给定级别的所有分数

    firestore
        .collection('scores')
        .where('level', '==', X)
        .orderBy('score')
        .orderBy('timestamp')
    

    或者类似的东西。您只需要一个复合索引即可支持此查询。

    【讨论】:

    • 有道理。看起来这可能会增加我的阅读次数,但看起来是唯一的选择。谢谢道格:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多