1

I'm trying to find a user by a value in an array. This is the structure of a user:

enter image description here

And I'm using this but it returns null:

FirebaseDatabase.getInstance().getReference().child("users")
                .orderByChild("tokens")
                .equalTo(from)

UPDATE: I've tried Puff suggestion and this is my structure now: enter image description here

The code I'm using is:

FirebaseDatabase.getInstance().getReference().child("users)
                .orderByKey()
                .equalTo(token)

But still I get null result.

UPDATE 2:

So what I'm trying to do is not possible. You can't create a query starting from parent and give in the child of child. As Puff said you can ONLY query direct reference, witch is not very useful in all cases. Still I managed to get what I wanted saving the value in another form, locally.

:(

Razvan Cristian Lung
  • 5,661
  • 5
  • 25
  • 49
  • 1
    There is no node called `users` in the JSON you showed. But aside from that: you cannot use queries to [detect whether a specific value exists in an array](http://stackoverflow.com/q/39149216). The closest you can get is storing the tokens as keys instead of values. But even that won't scale well. The best open is to stop nesting the tokens as shown here: http://stackoverflow.com/q/40656589 – Frank van Puffelen Mar 23 '17 at 16:14
  • if it is okay to change your database structure, then I think the easiest way to get what you want is by creating something like `user-tokens` that contains `token -> user` list – koceeng Mar 27 '17 at 06:54

0 Answers0