JSON数据显示各种学院中的可用学科:
{
"AvailableDisciplines": {
"4JN": {
"Disciplines": {
"CV": {
"disciplineCode": "CV",
"disciplineName": "Civil Engineering",
"siNo": "1"
},
"EC": {
"disciplineCode": "EC",
"disciplineName": "Electronics and Communication Engineering",
"siNo": "2"
},
"EE": {
"disciplineCode": "EE",
"disciplineName": "Electronics and Electricals Engineering",
"siNo": "3"
}
}
},
"4MT": {
"Disciplines": {
"EE": {
"disciplineCode": "EE",
"disciplineName": "Electronics and Electricals Engineering",
"siNo": "1"
},
"CS": {
"disciplineCode": "CS",
"disciplineName": "Computer Science Engineering",
"siNo": "2"
},
"IS": {
"disciplineCode": "IS",
"disciplineName": "Information Science Engineering",
"siNo": "3"
}
}
},
"1KT": {
"Disciplines": {
"TE": {
"disciplineCode": "TE",
"disciplineName": "Telecommunication Engineering",
"siNo": "1"
},
"CS": {
"disciplineCode": "CS",
"disciplineName": "Computer Science Engineering",
"siNo": "2"
},
"IS": {
"disciplineCode": "IS",
"disciplineName": "Information Science Engineering",
"siNo": "3"
}
}
}
}
}
其中4JN,4MT和1KT是学院代码。如果要查询所有提供土木工程学科的学院,则必须在AvailableDisciplines节点上写一个深层查询。查询代码为:
FirebaseDatabase.getInstance().getReference()
.child("Trial").child("AvailableDisciplines")
.orderByChild("Disciplines/EE").startAt("")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.v("log", "datasnapshot: " + String.valueOf(dataSnapshot));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}));
我能够按照我的期望获取数据。但是,在这种情况下,我不知道如何编写.indexOn
规则。你能帮我吗?
答案 0 :(得分:0)
这是示例规则代码: 正如您在下面看到的,密钥有多深无关紧要。
{
"rules": {
".read": true,
".write": "auth != null",
"AvailableDisciplines": {
"$otherKeys":{
".indexOn":["disciplineName"]
}
}
}
}
您可以在同一级别添加更多密钥,即:".indexOn":["disciplineName", "disciplineCode","siNo"]
答案 1 :(得分:0)
在当前数据模型中,您的查询应检查实际值(而不是节点的存在)。例如:
FirebaseDatabase.getInstance().getReference()
.child("Trial").child("AvailableDisciplines")
.orderByChild("Disciplines/EE/disciplineCode").equalTo("EE")
...
现在您有了一个实际的属性,可以在以下位置定义索引:
{
"rules": {
"Trial": {
"AvailableDisciplines": {
"$collegecode": {
"Disciplines": {
"$discipline": {
".indexOn": "disciplineCode"
}
}
}
}
}
}
}