【问题标题】:firebase .indexOn warning with autoId带有 autoId 的 firebase .indexOn 警告
【发布时间】:2016-11-09 21:29:24
【问题描述】:

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

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

我的 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 快照列表(类似于点赞数)。

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

我尝试了以下安全规则,但它不起作用

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

【问题讨论】:

    标签: swift indexing firebase firebase-realtime-database


    【解决方案1】:

    您很可能正在尝试向 cmets 查询特定帖子。

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

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

    有了这个,您可以根据 count 属性的值为特定帖子订购 cmets。

    【讨论】:

    • 非常感谢弗兰克。我从其他帖子中偶然发现了你的名字,希望你有时间看看你所做的!非常感激。我想了解更多关于 $ 符号以及语法如何工作的信息。你会推荐什么?谢谢
    • $ 只是意味着您在那里拥有的任何规则都适用于 each 子节点。然后,在该部分中,您可以使用 $commentId 来引用匹配的节点。要了解有关安全规则的所有信息,我强烈推荐来自 I/O 的the key to Firebase security(视频)
    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2018-12-14
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多