How to do order by child inside another child Firebase

佐手、 提交于 2020-11-29 02:53:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!