Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase DatabaseReference filter by specified values

Lets suppose I have this firebase JSON structure:

example

And I need to get all the questions which have the atribute "from" equal to "this".

I know that I can use Volley to make a StringRequest and GET all values from my questions.json. Then, in the client side I could iterate over the response and remove the ones which don't have the correct atribute, but this would only work if I have a few questions. If I have millions of questions I would need to iterate over those millions of questions.

Is there any way I could put a filter on my request to get only the question with an attribute of my choice? (Ex: search questions.json where from == this)

like image 736
Ravers Avatar asked May 25 '17 11:05

Ravers


People also ask

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do you filter data in Firebase Realtime Database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .

What does getKey () do Firebase?

Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value. Using Firebase to generate unique keys in this way is also of particular use when an app has multiple users creating child nodes at the same path in the tree.

What is orderByChild in Firebase?

Inherited from Query.orderByChild. Generates a new Query object ordered by the specified child key. Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query is an error. Firebase queries allow you to order your data by any child key on the fly.27-Jul-2022.


2 Answers

Try this if may be useful in your scenario

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    Query query = reference.child("questions").orderByChild("from").equalTo("this");
    query.addListenerForSingleValueEvent(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                for (DataSnapshot issue : dataSnapshot.getChildren()) {
                // do with your result
                } 
            } 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) {

        } 
    }); 
like image 172
ashish Avatar answered Sep 20 '22 23:09

ashish


You can use equalTo

Query query = reference.child("questions").orderByChild("from").equalTo("this");

like image 32
Dishonered Avatar answered Sep 21 '22 23:09

Dishonered