Firebase数据库高级查询选项

时间:2018-01-28 21:13:04

标签: java android firebase firebase-realtime-database

我的数据结构如下。基本上,我创建了一个“追随者”类别作为每个键的子项,其中一个键代表一个民意调查。如果我的应用程序的当前用户ID列在“关注者”类别中,我希望该节点与我的查询相关。否则,我希望它被忽略。

这种类型的查询是否可行?我从顶层开始,“民意调查”,然后我必须遍历每个子密钥,然后在该密钥内“跟随”子项:

    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Polls");

enter image description here

1 个答案:

答案 0 :(得分:1)

这种查询是可能的。

您只需要查看每个轮询对象,看看您关注的user_ID是否存在于关注者列表中。

另一个解决方案是让Followers节点根据user_ID将关注者链接到他们关注的民意调查。

这在这里有更详细的描述:

https://firebase.google.com/docs/database/android/structure-data#flatten_data_structures

如果将数据拆分为单独的路径(也称为非规范化),则可以根据需要在单独的调用中有效地下载数据。考虑这种扁平结构:

{
  // Chats contains only meta info about each conversation
  // stored under the chats's unique ID
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
    },
    "two": { ... },
    "three": { ... }
  },

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

  // Messages are separate from data we may want to iterate quickly
  // but still easily paginated and queried, and organized by chat
  // conversation ID
  "messages": {
    "one": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      },
      "m2": { ... },
      "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
  }
}