Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Query to exclude data based on a condition

I'm able to get the data for a particular value in the child using orderByChild and equalTo (cool that it works for nested child as well)

private void getData() {
        try {
            final DatabaseReference database = FirebaseDatabase.getInstance().getReference();

            database.child(Constants.TABLE_TASKS).orderByChild("user/id")
                    .equalTo("somevalue")
                    .addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Timber.d(dataSnapshot.toString());
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

Is there an easy way to get the data where a particular value is not found, basically something like a notEqualTo("somevalue") ?

like image 349
Monu Surana Avatar asked Aug 28 '16 19:08

Monu Surana


2 Answers

In the Firebase Query model you can not filter for inequality to a value.

But you can test for the absence of any value (essentially: the absence of a property). For example with this data model:

{
    child1: {
      "user": {
        "id": 1,
        "name": "puf"
      }
    },
    child2: {
      "user": {
        "id": 2,
        "name": "abe"
      }
    },
    child3: {
      "user": {
        "id": 3
      }
    }
}

I can query for children without a user/name property with:

ref.orderByChild('user/name').equalTo(null)

Which leads to the only child that doesn't have a name:

child3

Feel free to play with my jsbin to see if you get further: http://jsbin.com/liyibo/edit?js,console

Update: I knew I'd answered this before, but couldn't find it earlier. Here's the dupe: is it possible query data that are not equal to the specified condition?. It looks like I have a mistake in there, since clearly I'm testing for the absence of a property in the above code.

like image 127
Frank van Puffelen Avatar answered Sep 21 '22 09:09

Frank van Puffelen


I think I've found the solution and this is more of how the database should be designed and actually now I understood the intention behind Firebase guideline

https://firebase.google.com/docs/database/android/structure-data

Original Design:

{
    child1: {
      "user": {
        "id": "id1",
        "name": "puf"
      }
    },
    child2: {
      "user": {
        "id": "id2",
        "name": "abe"
      }
    },
    child3: {
      "user": {
        "id": "id1"
        "name": "puf"
      }
    }
}

Updated Design:

So apart from the storing the id and name of the user, we should also store a node with the id itself as the key and mark it to true

{
    child1: {
      "user": {
        "id": "id1",
        "name": "puf"
        "id1": true
      }
    },
    child2: {
      "user": {
        "id": "id2",
        "name": "abe"
        "id2": true
      }
    },
    child3: {
      "user": {
        "id": "id1"
        "name": "puf"
        "id1": true
      }
    }
}

With the updated design, if i execute ref.orderByChild('user/id1').equalTo(true)

I would get output as Child1 and Child 3

and if i execute ref.orderByChild('user/id1').equalTo(null),

I would get Child2 as the output

like image 39
Monu Surana Avatar answered Sep 20 '22 09:09

Monu Surana