使用autoId警告firebase .indexOn

时间:2016-07-07 14:04:39

标签: swift indexing firebase firebase-realtime-database

我正在使用firebase来存储一些数据。当我使用queryOrderedByChild(" count")时,我收到以下警告

使用未指定的索引。考虑添加" .indexOn":" count" at / comments / -KM449ubmviUkGNIj2fq您的安全规则以获得更好的性能

我的firebase结构如下所示

  "comments" : {
"-KM449ubmviUkGNIj2fq" : {
  "-KM44Ax8MdcBBPT_BQiO" : {
    "count" : 3,
    "comment" : "a",
    "commentOwner" : "hakV4smGyveurPhFN7g9rad4xsP2",
    "date" : "1467890319.38933"
  },
  "-KM45-Eb6yvXSAfhBrLX" : {
    "count" : 2,
    "comment" : "b",
    "commentOwner" : "hakV4smGyveurPhFN7g9rad4xsP2",
    "date" : "1467890533.56065"
  },
  "-KM488JCKddZhbev466U" : {
    "count" : 4,
    "comment" : "c",
    "commentOwner" : "hakV4smGyveurPhFN7g9rad4xsP2",
    "date" : "1467891357.06534"
  },
  "-KM48SXJ83L7-mpE5nma" : {
    "count" : 0,
    "comment" : "d",
    "commentOwner" : "hakV4smGyveurPhFN7g9rad4xsP2",
    "date" : "1467891439.88917"
  },
  "-KM4AN_rj-ycRZYESnh_" : {
    "count" : 0,
    "comment" : "e",
    "commentOwner" : "hakV4smGyveurPhFN7g9rad4xsP2",
    "date" : "1467891944.01086"
  },

基本上,我想生成一个具有最高计数(与喜欢的数量相似)的firebase快照列表。

由于我的帖子和评论都有他们从firebase生成的唯一ID,我不确定在索引方面我应该做些什么来消除警告。

我已尝试过以下安全规则但无效

  "rules": {
".read": "auth != true",
".write": "auth != true",
"comments" : {
  ".indexOn" : ["count"]
    }
}

1 个答案:

答案 0 :(得分:2)

您很可能正在尝试查询特定帖子的评论。

在这种情况下,您需要告诉数据库索引每个帖子的注释。其语法是:

"rules": {
  ".read": "auth != true",
  ".write": "auth != true",
  "comments" : {
    "$commentId": {
      ".indexOn" : ["count"]
    }
  }
}

通过这个,您可以按照count属性的值来订购特定帖子的评论。

相关问题