【问题标题】:How to retrieve multiple list of data from firebase如何从firebase检索多个数据列表
【发布时间】:2021-08-11 13:55:30
【问题描述】:

我一直在寻找不同的示例,但无法为我的 firebase 节点找到合适的解决方案。我有 3 个节点 1 用于问题,第二个节点包含问题的答案,第三个节点包含对该答案的评论。!

我将如何使用 firebase 执行查询 我一直在寻找不同的示例,但无法为我的火基表找到合适的解决方案。我有 3 个节点

  1. 问题
  2. 答案
  3. 对答案的评论

我将如何使用 firebase 执行查询(基于联接的概念实现)

mdatabaseReference.child("Answer").equalTo(QID);

我将如何获得特定问题的答案以及对该答案的评论!

这是我的 JSON

    {
      "Answer" : {

        "f40357b1-d1f5-4b7a-98ec-54d9e7b2e518" : {

          "dateTime" : "16 Mar 2017 15:30:29",
          "professorAnswer" : "Hezbollah is an Islamist religious organization founded in 1985 and based in Lebanon. It follows Shi'Islam (also called Shi'ite Islam), the second largest denomination of Islam. Shi'a Muslims follow the teachings of the prophet Muhammad, a direct descendant of Isma'il (the first son of Ibrahim/Abraham).Contd.!",
          "professorId" : "7ceef713-eb59-4db4-a8d2-21d5a01eedfc",
          "questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2"
        }
      },

      "comment" : {
        "29192e3a-a013-4fcc-9859-1f5cc62464cb" : {
          "commentText" : "ORGANIZATION hezbollah bases on the bible but their goals is to save people in pagans work!",
          "dateTime" : "16 Mar 2017 15:30:52",
 "AnswerId" : "f40357b1-d1f5-4b7a-98ec-54d9e7b2e518"
          "questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
          "userId" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
        }
      },



 "questions" : {

    "41c454a8-fab6-4e41-9093-b1120ffd1be0" : {
      "description" : "I know they're a Islamic organization but where are they based? What are their goals?",
      "idQuestion" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
      "time" : "16 Mar 2017 15:30:12",
      "title" : "What are the aims of the religious organization Hezbollah?",
      "user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2",

    }
  },



 "user" : {

    "13bd37e5-fc87-4468-a89e-7cb4ecaab05f" : {

      "email" : "email@gmail.com ",
      "user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
    }
}

问题是我想过滤这些事件......例如,使用 .orderByChild("Answer").equalTo(QID) 可能是错误的查询,但它只是为了获取给定问题 ID 的答案然后填充我的列表它。

【问题讨论】:

  • 与其用文字描述你的数据结构,不如分享一个 JSON 的代表性 sn-p。您可以通过单击 Firebase Database console 中的导出 JSON 来获取此信息。
  • 等待您的回复! @弗兰克·范·普菲伦

标签: android firebase firebase-realtime-database


【解决方案1】:

这样做的正确方法是分别获取每个数据,我不完全了解您的数据是如何组织的,但这可能是一个合适的解决方案:

// Gets the Question with id = QID
mdatabaseReference.child("Questions").child(QID);

// Gets the Answers for that question
mdatabaseReference.child("Answers").child(QID).once("value", function(answers) {

    // For every Answer gets the comments
    for(var answerID in answers) mdatabaseReference.child("Comments").child(QID).child(answerID);
});

编辑:要有效地使用 Firebase,您应该根据您想要检索它们的方式来构建数据。如果您想获得给定问题的所有答案,我建议您使用此数据结构:

{
    "Answers": {
        "questionID": {
            "answerID": {
                "dateTime" : "16 Mar 2017 15:30:29",
                "professorAnswer" : "Hezbollah is an Islamist religious...,
                "professorID" : "...",
                "questionID" : "..."
            }
        }
    }
}

这样您就可以通过这种方式获取给定 questionID 的数据:

mdatabaseReference.child("Answer").child(questionID).once('value', function(answers) {
    // answers contains all the answer for the question with ID == questionID
});

注意:您在 Firebase 中没有表,一切都是 JSON 对象

【讨论】:

  • 我想知道有没有加入 Fire base 的概念。?其中 1 只运行一次查询,它会显示特定问题的答案和 cmets。?
  • 没有联接的概念,最好单独获取数据片段并根据您想要检索它们的方式组织数据。如果您真的想获得答案和评论,您可以将您的 cmets 作为答案的孩子,但我建议您尝试单独获取您的数据,它是高性能的,并且是在 Firebase 中执行此操作的正确方法:) @987654321 @真的很有用
  • 但我不认为这是一个很好的做法,如果数据量变得很大怎么办。?然后它将加载每个表数据,然后比较该数据这会使我的应用程序变慢。?
  • 使用这种方法,您只需获取您需要的数据。例如,如果您有 2000 个问题,您只会收到带有id == QID 的问题。这实际上取决于您如何构建数据,我已经用一个针对您的问题的良好数据结构的示例更新了我的答案。如果您还有其他问题,请告诉我 ;)
  • 告诉我一件事。?
【解决方案2】:

fireBase 中没有连接的概念。它的 noSql ..您需要做的就是根据问题 ID 查询,然后在答案 ID 中检查问题 ID..对于 cmets 也是如此,它将是一个嵌套基于事件的搜索!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多