我一直在寻找不同的例子,但无法找到适合我的火力基地表的解决方案。我有3个表1表示问题,第二个表包含问题的答案,第3个表包含对该答案的评论。!
我将如何使用firebase执行查询 我一直在寻找不同的例子,但无法为我的火基表找到合适的解决方案。我有3张桌子
我将如何使用firebase执行查询
mdatabaseReference.child("Answer").equalTo(QID);
我将如何在答案中获得特定问题和评论的答案。!
这是My 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的答案,然后填充我的列出它。
答案 0 :(得分:4)
这样做的正确方法是单独获取每个数据,我不完全了解您的数据是如何组织的,但这可能是一个合适的解决方案:
// 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对象
答案 1 :(得分:2)
在没有加入概念的情况下在fireBase.Its noSql ..你需要做的是根据问题ID查询然后在回答id检查问题id ..同样的事情是评论它将是一个嵌套基于事件的搜索。!