0

I have the following structure of data in the firebase database.

tripPackages
  -LCoHHZWL-NTLmJKDILV
    50180557210016753
      creationDate:
      farmerID:
      id:
      itemState:
      noOfItems:
      noOfSoldItems:
      totalAmount:
      tripId:
      tvoID:
      vegetableName:
      vid
      weight:

Node trippackages contains trips which contain packages, where -LCoHHZWL-NTLmJKDILV is a trip id, and 50180557210016753 is a package id. Now, I want to filter all the trippackages with a particular itemState (say IN_TRASIT).

I believe this code should do this:

firebaseDatabase.getReference().child("tripPackages").orderByChild("itemState").equalTo("IN_TRANSIT");

where value of child itemState is an enum in my POJO, but I am not able to achieve the desired results.

Does the nested location level of child or type of the value of child in POJO has anything to do with it?

I am able to successfully filter data in similar data structure when the nested location level of child on which orderByChild used is 2 and value of child is a String.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Krishna
  • 770
  • 2
  • 8
  • 21

2 Answers2

1

Does the nested location level of child or type of the value of child in POJO has anything to do with it?

Yes, it affects. You have this database:

tripPackages  
   -LCoHHZWL-NTLmJKDILV
           50180557210016753
                   itemState: IN_TRANSIT

To be able to filter according to the value of itemState, try the following:

firebaseDatabase.getReference().child("tripPackages").child("-LCoHHZWL-NTLmJKDILV").orderByChild("itemState").equalTo("IN_TRANSIT");

You need to add the trip id, to be able to filter the data according to the itemState

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I can not do that as I do not know the pushIDs (`.child("pushID")`) where I fetch the data. I just want all the packages with `itemState:"IN_TRANSIT"` under the node `tripPackages`, and I do not have IDs for all the trips (immediate child of the node `tripPackages`). – Krishna Jun 23 '18 at 07:48
  • Can you please direct me to the docs that talk about the limitations of `orderByChild` or `equalTo` in relation to the nested location level of the data. – Krishna Jun 23 '18 at 07:50
  • Can you access this `50180557210016753`? – Peter Haddad Jun 23 '18 at 07:55
  • if you can try this then, `firebaseDatabase.getReference().child("tripPackages").orderByChild("50180557210016753/itemState").equalTo("IN_TRANSIT");` – Peter Haddad Jun 23 '18 at 07:56
  • This is the docs: https://firebase.google.com/docs/database/android/lists-of-data#sort_data (not sure if it talks about the limitation that much). But when you use `orderByChild` you can skip one node, as I did in the answer and in the comment above this one. – Peter Haddad Jun 23 '18 at 07:57
  • Sorry, but I do not have the access to the package Id as well. – Krishna Jun 23 '18 at 08:07
  • Docs do not discuss anything about such limitations. I have spent considerable amount of time on it, and I have read docs thoroughly, I think :P. – Krishna Jun 23 '18 at 08:10
  • yes but I'm telling you, I'm the docs :p. If you cannot access both ids then how are you gonna access the `itemState`? – Peter Haddad Jun 23 '18 at 08:10
0

Firebase Database can only query children one level under a specific location. So you can query the packages of an individual trip (by querying on /tripPackages/-LCoHHZWL-NTLmJKDILV), but not across all trips.

Typical solutions are to store the packages as a list and give them a trip ID property, or to store an additional list of "package IDs by state".

Also see my answers here, which both contain a good example of the data structure to solve this:

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