是否有任何可能的方法来生成孩子的孩子的结果 喜欢我在单个问题上寻找多个答案。!
"questions" : {
"Q1(Random ID )" : {
"description" : "Deail of Question",
"idQuestion" : "Q1",
"time" : "17 Mar 2017 16:18:12",
"title" : "Title Of Question",
"user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
},
{
"Answer" : {
"81d19e85-8c6c-4824-9fb1-61a7cd316e32" : {
"dateTime" : "17 Mar 2017 16:22:17",
"Answer" : "Answer",
"ProfessorId" : "da587c89-f001-43f2-984d-e6dbac491acc",
"questionId" : "Q1"
},
"e2ec0556-4420-4d2f-b346-0b5cebeb1ebe" : {
"dateTime" : "17 Mar 2017 16:55:27",
"Answer" : "Answer 2 on Question 1",
"answerId" : "0d20d6df-907d-4133-be53-ca6acf6c2ad1",
"questionId" : "Q1"
}
},
随机生成的节点。 ! 我想做这样的事情。!
mdatabaseReference.child("Answer").child(* Here Im Using "*" Like we used to do in SQl to get all data I knw its not a SQL but its just for conecpt).equalTo(QID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (Answer_List.size() > 0)
Answer_List.clear();
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
而不是使用postSnapshot.getKey().equals("QID")
if(postSnapshot.getKey().equals("QID"))
Answertext = postSnapshot.getValue().toString();
这会让性能变得更慢,我正在寻找更快的东西。!
寻找类似的东西
mdatabaseReference.child("Answer").child("*").getKey().equals(QID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
答案 0 :(得分:1)
您正在寻找Firebase Database queries。
mdatabaseReference
.child("Answer")
.orderByChild("questionId")
.equalTo("Q1")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot answerSnapshot: dataSnapshot.getChildren()) {
System.out.println(answerSnapshot.child("Answer").getValue());
}
}
...
但是,虽然这样可行,但我强烈建议您更改数据模型,以使此类查询更具可扩展性。由于答案属于问题,因此您应该对该层次结构建模。虽然不应该混合问题和答案,但应考虑在问题ID下对答案进行建模:
"questions" : {
"Q1(Random ID )" : {
"description" : "Deail of Question",
"idQuestion" : "Q1",
"time" : "17 Mar 2017 16:18:12",
"title" : "Title Of Question",
"user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
},
}
"Answer" : {
"Q1": {
"81d19e85-8c6c-4824-9fb1-61a7cd316e32" : {
"dateTime" : "17 Mar 2017 16:22:17",
"Answer" : "Answer",
"ProfessorId" : "da587c89-f001-43f2-984d-e6dbac491acc",
},
"e2ec0556-4420-4d2f-b346-0b5cebeb1ebe" : {
"dateTime" : "17 Mar 2017 16:55:27",
"Answer" : "Answer 2 on Question 1",
"answerId" : "0d20d6df-907d-4133-be53-ca6acf6c2ad1",
}
}
}
通过这种结构,您可以通过直接查找而不是查询(在每个增长列表中)获得Q1的答案:
mdatabaseReference
.child("Answer")
.child("Q1")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot answerSnapshot: dataSnapshot.getChildren()) {
System.out.println(answerSnapshot.child("Answer").getValue());
}
}
...