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?

10
  • Where part contains 3 childrens or one of the part's childrens has 3 as a value? Commented Jan 22, 2018 at 12:02
  • its probably better if you change the structure of the database Commented Jan 22, 2018 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. Commented Jan 22, 2018 at 12:09
  • Where part contain 3 as a value. Commented Jan 22, 2018 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 Commented Jan 22, 2018 at 12:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0
 Query query = databaseReference.orderByChild("databaseData").equalTo("yourdatayouwant"); 

You can do that with this way

1 Comment

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 .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.