Firebase实时数据库-包含uid的位置上的索引

Firebase实时数据库-包含uid的位置上的索引,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,我正试图通过按照控制台日志中的建议创建索引来提高Firebase实时数据库的性能。大多数建议很容易遵循,但不是全部 我的现行规则: "notes": { ".indexOn": ["data/title", "access/author"], ".read": " auth.uid !== null ", "$note_id": { ".write": " (!data.exists() && auth.uid !== null) |

我正试图通过按照控制台日志中的建议创建索引来提高Firebase实时数据库的性能。大多数建议很容易遵循,但不是全部

我的现行规则:

"notes": {
  ".indexOn": ["data/title", "access/author"],
  ".read": "
    auth.uid !== null 
  ",
  "$note_id": {
    ".write": "
      (!data.exists() && auth.uid !== null) ||
      (
        data.child('access').child('author').val() === auth.uid 
      ||
        data.child('access/members').child(auth.uid).exists()
      )
    ",
    "data": {
      ".read": "
        data.parent().child('access').child('author').val() === auth.uid ||
        data.parent().child('access/members').child(auth.uid).exists()
      ",
      ".write": "
        data.parent().child('access').child('author').val() === auth.uid ||
        data.parent().child('access/members').child(auth.uid).exists()          
      ",
    "access" : {
      ".read" : "
        (auth.uid !== null) &&
        (
          data.child('author').val() === auth.uid
          ||
          data.child('members').child(auth.uid).exists()
        )
      ",
    "members" :{
        ".write" : "
          (!data.exists() && auth.uid !== null) ||
          data.parent().child('author').val() === auth.uid ||
          data.parent().child(auth.uid).exists()
        "
      }
    }
  }
},
一些建议适用于以用户uid结尾的位置-类似于下面的控制台日志:

FIREBASE警告:使用未指定的索引

考虑添加.indexOn:access/members/3weT1tYhG456HH0CuC45Muc44ax6 at/notes 更改您的安全规则以获得更好的性能


考虑到位置以用户uid字符串结尾,是否可以在Firebase实时数据库中添加此索引规则?

您当前的数据结构可以轻松找到特定注释的成员。它不便于查找特定成员的注释。为了允许第二个用例并确保其可扩展,您需要添加一个额外的数据结构:

user_notes: {
  user_id_1: {
    note_id1: true,
    note_id2: true,
    note_id3: true
  },
  user_id_2: {
    note_id3: true,
    note_id4: true,
    note_id5: true
  }
}
使用此附加结构,您可以确定用户可以访问哪些节点,而无需查询

当然,这意味着当您允许用户访问便笺时,您需要向两个位置写入内容,但这是非常常见和可扩展的,并且仍然可以通过规则进行保护

另见:


感谢@frank的及时回复和极好的回答!:这似乎是一个足够简单的解决方案。我已经准备好了数据库侦听器来完成这项工作。现在我只是想知道我将如何以有效的方式获取这些注释,而不仅仅是id?顺便问一下,您是否建议同时从客户端写入数据以更新用户\u notes节点或使用数据库侦听器?获取具有上述结构的实际节点意味着您将为每个节点使用一个侦听器。这并不像你最初想象的那么慢,因为Firebase。当然,你也可以在两个地方写下整个便笺,这会复制数据,但会使读取更简单、更快。