Firebase DatabaseReference filter by given values

Suppose I have this JSON structure for Firebase:

example

And I need to get all questions that have a "from" attribute equal to "this".

I know that I can use Volley to create a StringRequest and get all the values ​​from my questions. json. Then on the client side I could iterate over the answer and remove the ones that don't have the correct attribute, but that would only work if I have multiple questions. If I have millions of questions, I will have to iterate over these millions of questions.

Is there some way to put a filter on my query to only get a question with an attribute of my choice? (For example: search questions.json where from == this)

+3


source to share


2 answers


Try this if it might be helpful 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) {

        } 
    }); 

      

+10


source


You can use equalTo



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

+2


source







All Articles