How to retreive the child Nodes keys only Firebase?

enter image description here

Actually i am fetching the data of all the subjects to the listView

which are Pathology and SocialScience.

when i am retreiving the key i am getting ending date , starting date all that too.

How can i get the only the children which have nodes like here pathology and Soil scence. ?

Answer

When you retrieve a node with Firebase, it always retrieves the entire node. There is no way to get a node without its children, or (beyond what a query can do) with a subset of its children.

When you have this need, it typically means that you’ve mixed two types of data in a single branch. The Firebase documentation recommends avoiding such data nesting.

In your case, I’d split the data into two top-level nodes, each with the same keys for its children. Something like:

ExamInfos: {
  "-MV8_...": {
    "endingDate": ...,
    "examType": ...,
    "programme": ...,
    "startingDate": ...
  }
},
ExamSugbjects: {
  "-MV8_...": {
    "PATHOLOGY": { ... },
    "SOIL SCIENCE": { ... }
  }
}

Now you can get all subjects for an exam, by loading its ExamSubjects child node, all other info by loading its ExamInfos child node, and you can load both if needed. Contrary to what many devs initially expect, this won’t take much more time, as Firebase can pipeline the requests over its existing web socket connection.


As a separate recommendation, I’d also consider storing the dates in a format that allows more ordering and filtering. The most common format for this is 2021-03-07 for March 7, 2021.

Leave a Reply

Your email address will not be published. Required fields are marked *