JDKSoftDev
JDKSoftDev

Reputation: 103

How to query inside all children of a node on firebase?

How can I query past my user id's to compare everyone's stats?

My firebase database is such:

   " <URL>.firebase.com/" {
        "users/"
            "facebook:283638484/"
                "name/"
                "tokens/"
                "spins/"
            "facebook:283478133/"
                "name/"
                "tokens/"
                "spins/"

Same question reworded, can I search through all "facebook:38362728", etc, users to get a list of the top three users with the highest tokens?

I want to create a leaderboard in my app. The users are all saved by their userID, and have their personal stats in each. I want to check the value of "tokens" inside each user, and order them so the top three are shown. Is there a way to like, wildcard for the uid in the url so I can check them all? And if/when the app grows to a large size, will querying every user to populate this list, take to long? I read about .indexOn, but wasnt sure how this fit in considering the node structure. Thanks in advance!

Upvotes: 1

Views: 712

Answers (1)

Jay
Jay

Reputation: 35648

If I understand the question correctly I'll provide a simplified structure:

users
  user_id_0
    tokens: 20
  user_id_1
    tokens: 5
  user_id_2
    tokens: 12

Then to retrieve the first two (based on sorted order)

Query queryRef = ref.orderByChild("tokens").limitToFirst(2);
queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
        System.out.println(snapshot.getValue()); //?
    }
    // ....
});

I am not an android developer so my apologies for incorrect code structure.

Upvotes: 1

Related Questions