0

I am working with Firebase in Android. In my app, there is a scenario where I need to fetch the filtered list from root node on basis of a field which is inside its child. For more clearance on problem see the sample database structure below.

enter image description here

For example, I need to fetch from Root Chats where part contains 3 as value. I have tried Firebase Query but not helpful. Is there a way to get the filtered list as above?

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
Diaz diaz
  • 284
  • 1
  • 7
  • 21
  • Where part contains 3 childrens or one of the part's childrens has `3` as a value? – Alex Mamo Jan 22 '18 at 12:02
  • its probably better if you change the structure of the database – Peter Haddad Jan 22 '18 at 12:09
  • you should use firebase's structure for realtime-database chats to make a working chatroom with easy querying. anyway, i recomend switching to firebase firestore that does have a quite better querying system that might satisfy you. – CptEric Jan 22 '18 at 12:09
  • Where part contain 3 as a value. – Diaz diaz Jan 22 '18 at 12:09
  • its really better if you change the structure, as if you only have the child("chats") then it will be a complex query or it wont even work. But if you have other childrens avaliable then it can work – Peter Haddad Jan 22 '18 at 12:16
  • So i guess there is no way to perform such query . Can you please provide some link or reference for a better ChatModule database structure which i can follow . – Diaz diaz Jan 22 '18 at 12:19
  • I suggest you change your structure that part will be string of values separated by comma then you can do something like this `databaseReference.orderByChild('part').startAt("3").endAt("3"+"\uf8ff").once("value")` – Ali Faris Jan 22 '18 at 12:24
  • why is there "0" and "1" under the parts anyway? What is the parts also? – Peter Haddad Jan 22 '18 at 12:28
  • parts is an Array thats why there is indexing as 0,1... . – Diaz diaz Jan 22 '18 at 12:42
  • This is a categorization problem, which indeed can't be solved with your current data structure. Have a look at my explanation here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jan 22 '18 at 15:25

2 Answers2

2

You cannot query your database as mentioned in your question using the actual database structure. In such cases, there is a practice named denormalization and for that I recomend you see this video, Denormalization is normal with the Firebase Database.

In your case, you need to get the part node out from chat node and create a new top level node in which you can store those values. I recomend you also use as the name of the child, the exact name that you want to query. In this case you will be able to attach a listener on that particular node and use exists() method to check a child for existens.

In also recomend you read this post, Structuring your Firebase Data correctly for a Complex App for a better understanding.

There are two more resourses that I want to share, The Firebase Database For SQL Developers and NoSQL Data Modeling Techniques.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0
 Query query = databaseReference.orderByChild("databaseData").equalTo("yourdatayouwant");

You can do that with this way

Yıldırım
  • 757
  • 10
  • 24
  • I guess not `equalTo` can work with single value type . But As i mentioned in question parts is an array so you can not perform `equalTo` on an array . – Diaz diaz Jan 22 '18 at 12:46