如何解决Firebase实时数据库的索引问题

时间:2019-06-18 01:21:22

标签: firebase-realtime-database

我有索引的规则,但是当数据很大时,仍然出现以下错误。

错误为@ firebase / database:FIREBASE警告:使用未指定的索引。您的数据将在客户端上下载并过滤。考虑将/ history / bearhistory的“ .indexOn”:“ time”添加到安全规则中,以获得更好的性能。

  "rules": {
     ".read": true,
      ".write": true,
        "bearhistory": {
      ".indexOn": ["time"]
    },
      "history": {
      ".indexOn": ["time"]
    }

  }

}

正如我所说,当我使用最小limitToLast时,它可以工作。

this.items = this.db.list(`history/bearhistory`, ref => ref.orderByChild("time").limitToLast(10000));

当数据limitToLast较大时,我得到这些索引错误。我有这个数据结构。

Data Set

如何解决该错误以处理索引?

1 个答案:

答案 0 :(得分:0)

如错误消息所述,查询正在/history/bearhistory上运行,因此您需要在其中定义索引:

"rules": {
   ".read": true,
   ".write": true,
   "history": {
      ".indexOn": ["time"],
      "bearhistory": {
        ".indexOn": ["time"]
      }
  }
}