1

Let's say that we have simple data structure:

"games": {
     "{gameId}" {
          "title": "Chess",
          "playedBy" : {
               "{userId}" : true,
               "{userId}" : true
           }
     }
}

There are just games and every game has data about users who played in this game. Now I want to get all games where specified user (let's say his id is "1234567") didn't play. I have to query games where "playedBy" does not contain "1234567".

How can I achieve that with Firebase?

Every language where Firebase works will be appreciated.

Nominalista
  • 4,632
  • 11
  • 43
  • 102
  • 2
    As far as I know there is no support for "not equals" operator in Firebase for now. –  Ekalips May 03 '17 at 11:38
  • Please check with this http://stackoverflow.com/questions/37752402/firebase-database-not-equal-request-alternative-solution-for-ios –  May 03 '17 at 11:48
  • Firebase can only query for values that are present. See http://stackoverflow.com/questions/28582223/is-it-possible-query-data-that-are-not-equal-to-the-specified-condition – Frank van Puffelen May 03 '17 at 14:01

1 Answers1

1

You can do it like this:

  1. Iterate trough all the games
  2. Check if each game contains the user id
  3. If not , add the game to list of games

    mGamesRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
             if(!dataSnapshot.child("playedBy").hasChild(userId)) {
                 gamesList.add(dataSnapshot.getValue(Game.class));
             }
        }
        ...
    

Hope this works for you.

Hristo Stoyanov
  • 1,960
  • 1
  • 12
  • 24