1

I am trying to fetch data from Firebase Real-time database using where condition. I want to get all data based on a specific keyword. please my JSON data below.

enter image description here

I want to fetch data where a company is equal to Balaji Seeds. And I want to print proname,proid.

I tried below code.

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference mDatabaseReference = database.getReference();
    Query query = mDatabaseReference.child("Products").orderByChild("company").equalTo("Balaji Seeds");


        query.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String previousChildKey) {
                Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();

                Toast.makeText(CompanyCategoryList.this, "ProName: "+newPost.get("proname"),Toast.LENGTH_LONG).show();


            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }


        });

I hope you understand my concern. Thanks in Advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Asesha George
  • 2,232
  • 2
  • 32
  • 68

1 Answers1

0

Firebase Realtime Database queries operate on a list of child nodes. Your data structure is deeply nested, which makes it harder to query.

In your current data model, you can run a query on /Products/Vegetable_Seeds/Chili and then get the child nodes nodes under that where company is equal to Balaji Seeds. But the data model does not allow querying across all Products for that node.

If you want to allow this query, you'll either have to change your current model to be a flat list, or add an additional data structure where you keep the data needs for the look up of the products for a company.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807