Fajar Syairillah
Fajar Syairillah

Reputation: 93

How to get data based on value from firebase database

i'm trying to get the data based on "Payments" in child "State" in my structured data My Structured firebase

I used this

salesRef = FirebaseDatabase.getInstance().getReference()
                .child("Orders")
                .orderByKey().startAt("Payments").endAt("Payments"+"\uf8ff");

but i got nothing back.What was wrong ? What should i use?

Upvotes: 3

Views: 836

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1499860

You're ordering by key (the 089630116543 etc) and then saying you want keys starting at "Payments" and ending at "Payments\uf8ff". There aren't any such keys, which is why you're not getting back any results.

I haven't used Firebase Realtime Database myself (just Firestore, from C#, which is related but not quite the same). I suspect if you change orderByKey() to orderByChild("state") that would fix it:

salesRef = FirebaseDatabase.getInstance().getReference()
    .child("Orders")
    .orderByChild("state").startAt("Payments").endAt("Payments\uf8ff");

Note that this won't find the value where State is "Waiting Payments Config" because that doesn't start with Payments. If you actually want a "contains" kind of query, that could be trickier (or infeasible, I'm not sure).

Upvotes: 4

Peter Haddad
Peter Haddad

Reputation: 80914

You need to do the following:

salesRef = FirebaseDatabase.getInstance().getReference().child("Orders").orderByChild("state").equalTo("Waiting Payments Config");

orderByChild will take the name of an attribute as an argument and, equalTo will take the value of that attribute as an argument.

Check the docs:

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

Upvotes: 5

Related Questions