问题
I am trying to get all data ordered asc and desc by child value that inside another child. In main scope of class:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbRef = database.getReference("university");
i have data structure like this
{
"university": {
"13": {
"name": "value"
"exam": {
"exam_date": "value"
"reg_date": "value"
}
}
"15": {
"name": "value"
"exam": {
"exam_date": "value"
"reg_date": "value"
}
}
"20": {
"name": "value"
"exam": {
"exam_date": "value"
"reg_date": "value"
}
}
}
}
First I try this way Query q=dbRef.orderByChild("13/exam/exam_date")
but it is getting data like when i use ValueEventListener with dbRef directly without ordering.
回答1:
To solve this, please change the following line of code:
Query q=dbRef.orderByChild("13/exam/exam_date")
with
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("university").orderByChild("exam/exam_date");
回答2:
firebaseDatabaseRef.child(AppConstants.RootNode.userRoomGroupIds).child(myUserId).orderByChild(AppConstants.FireBaseRoom.LAST_MESSAGE+"/"+AppConstants.LASTMESSAGE.MESSAGE_TIME_STAMP).startAt(System.currentTimeMillis());
This query give data from next to my current milisec so i use orderByChild in which give refrence to particular child /lastmessage/timeStamp
来源:https://stackoverflow.com/questions/49445352/how-to-do-order-by-child-inside-another-child-firebase