Firebase: Query to exclude data based on a condition

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.


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