1

I've been trying to find a way to query by the child node 'votes' to order the data by number of 'votes'. I'm pretty sure because of the way I have stored the data in firebase it cant be queried but I currently have to store it this way.

  {
     "45" : {
       "Biological and Ecological" : {
         "Votes" : 1
        }
    },
     "567" : {
       "Technology" : {
         "Votes" : 2
        }
    },
     "620" : {
       "Technology" : {
          "Votes" : 1
        }
  },
     "702" : {
       "Social and Behavioral" : {
          "Votes" : 1
        }

     }
 }

I am attempting to order by the child "Votes" is there a way to do this? Below is the method in which it is currently displayed I am aware that .OrderbyChild("Votes") isnt defining the path correctly but is there a way to achieve this?

   Query query = FirebaseDatabase.getInstance().getReference("CountLikes/").orderByChild("Votes");

   query.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for(DataSnapshot child: dataSnapshot.getChildren()) {

                for(DataSnapshot grandchild: child.getChildren()) {
                    arrayList.add("Project " + child.getKey() + "         " + grandchild.getKey() + "        " + (String.valueOf(grandchild.child("Votes").getValue(Long.class)))); 

                    adapter.notifyDataSetChanged();


                }

                }}



        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi! "Isn't defining the path correctly but is there a way to achieve this". Have you tried it? – Naya Feb 05 '19 at 10:46

3 Answers3

1

This depends a bit on how you're data model is actually set up in the RealtimeDatabase. In this case, there is not a consistent path to the data you're attempting to sort by (different vote types it seems).

If you have something like:

CountLikes: [ // collection
    docId: { // document
        typeOfVote: { // map name
            "votes": number // vote count
        }
    }
]

The way you are trying to order by the child will be possible if "votes" is split up from "typeOfVote" because right now you can't order by child through a potential wildcard (to get a level deeper than "Technology" for example).

CountLikes: [
    docId: {
        "votes": number,
        "type": typeOfVote
    }
]

With this, your orderByChild "votes" could probably work :)

FirebaseDatabase.getInstance().getReference("CountLikes/").orderByChild("votes")

(update) This answer by Frank (and read the comments) shows another example/more info for orderByChild.

0

The Firebase Realtime Database can query child nodes of a location based on a property, as long as that property is at a known location with a fixed path.

In your case, the child nodes are not at a fixed path (since the Technology and Biological and Ecological keys are not fixed), so this query is not possible with your current data structure.

To allow the query, restructure your data to turn it into a flat list. For example, this might work:

 "45" : {
   "Category": "Biological and Ecological",
   "Votes" : 1
 }
 "567" : {
   "Category": "Technology",
   "Votes" : 2
 },
 "620" : {
   "Category": "Technology",
    "Votes" : 1
 },
 "702" : {
   "Category": "Social and Behavioral",
   "Votes" : 1
 }

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

do you know all the possible values of the property like 'Biological and Ecological'? then you can just a for loop and select ....gerReference("CountLikes").orderbychild(property[i]+"/votes").....

I Know it's a weird way, but as you a told you cant change the structure,even I will suggest you to keep another dataset in the db for the values of the property

Apurba A
  • 71
  • 6