有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Firebase数据库高级查询选项

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

这种类型的查询可能吗?我从我的顶层“Polls”开始,然后我必须遍历每个子键,然后在该键中遍历“Following”子键:

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

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    这种查询是可能的

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

    另一个解决方案是保留一个Followers节点,该节点根据用户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": { ... }
      }
    }