AndroidDeveloper
AndroidDeveloper

Reputation: 155

Android Firebase Query to find by key without value

I want to create a query that will get me all elements that has "2005" under categories list.

I've tried this deep nesting but it got me all results (both 2005 and 2006)

Query queryRef = myRef.orderByChild("categories/2005");

The reason I want to do it with Query and not eventlistener is because the list is big and I want to do as much as filtering as i can on the cloud.

Sorry but I haven't found a solution for this on other posts. I could only find how to query by value.

DB structure

Upvotes: 0

Views: 303

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

It's a bit non-intuitive, but this works:

Query query = ref.orderByChild("categories/2005")
                 .startAt("-");

The trick is to pick a start value that is lower in the ASCII table than 0. I picked - because it reads nicely, but if you have a broader range of values you'll want to use a space as starting point, which is the lowest printable character.

Working jsbin: https://jsbin.com/viyibij/edit?js,console

Upvotes: 1

Related Questions