Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only get child keys in Firebase

Firebase database

I want to display only children of location i.e Sindh and Punjab (not their children). Is it possible, and if so how can I do it?

like image 223
Shahzain ali Avatar asked Jun 15 '17 07:06

Shahzain ali


People also ask

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do I get the key in Firebase?

Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

How do you filter data in Firebase Realtime Database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .


1 Answers

From Best practices for data structure in the docs:

Avoid nesting data

Because the Firebase Realtime Database allows nesting data up to 32 levels deep, you might be tempted to think that this should be the default structure. However, when you fetch data at a location in your database, you also retrieve all of its child nodes. In addition, when you grant someone read or write access at a node in your database, you also grant them access to all data under that node. Therefore, in practice, it's best to keep your data structure as flat as possible.

That is how Firebase works: If you get an item, you get its children as well. If you don't want this, you should restructure the database.

like image 100
nhaarman Avatar answered Oct 04 '22 03:10

nhaarman