Carl Bruiners
Carl Bruiners

Reputation: 546

Querying firebase database with a nested value

Having some challenges querying my firebase database for an Android app I'm writing, I have the following structure;

Matches
 - Match_01
  - awayScore
  - awayTeam
  - date
  - gameID
  - homeScore
  - homeTeam
  - pitch
  - played
  - time
 - Match_02
  - awayScore
  - awayTeam
  - date
  - gameID
  - homeScore
  - homeTeam
  - pitch
  - played
  - time

I want to be able to query by using the gameID field, the problem I'm having is how to reference the gameID without explicitly setting a child("Match_01"), as I have Match_02, Match_03, etc... below is the code I'm playing with (which doesn't work, but gives you an idea of what I want to achieve). GetArgument is a value I'm passing into my fragment;

    DatabaseReference ref = database.child("Matches");
    Query gameQuery = ref.child().child("gameID").equalTo(getArgument);

Any help is appreciated.

Upvotes: 2

Views: 307

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Yes it can be done like this:

 DatabaseReference ref = database.child("Matches");
Query gameQuery = ref.orderByChild("gameID").equalTo(getArgument);

when you use orderByChild() you do not have to specify the direct child under Matches

Upvotes: 2

Related Questions