Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase retrieve child keys but not values

Is there a way to get a list of keys of all child nodes (either once or with an open connection), without transferring all the data for those child nodes?

like image 548
Linda H Avatar asked Sep 07 '15 16:09

Linda H


People also ask

What is getKey () in Firebase?

getKey() returns the key (last part of the path) of the location of the Snapshot. getReference() returns the Reference for the location that generated this Snapshot. getValue() returns the data contained in this Snapshot. hasChild() returns true if the specified child path has (non-null) data.

Is Firebase key value?

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

How do I get DataSnapshot data?

public DataSnapshot child (String path)Get a DataSnapshot for the location at the specified relative path. The relative path can either be a simple child key (e.g. 'fred') or a deeper slash-separated path (e.g. 'fred/name/first'). If the child location has no data, an empty DataSnapshot is returned.

How can I check if a value exists already in a Firebase data class Android?

child(busNum). exists() tests for the existence of a value at location BusNumber/<busNum> . It will not be true unless busNum is one of the keys created by push() .


1 Answers

The Firebase JavaScript SDK always retrieves complete nodes, so there is no way to read only the keys.

The Firebase REST API has a parameter shallow=true that will retrieve only the keys under the location. See https://firebase.google.com/docs/database/rest/retrieve-data#shallow


If you don't want to use the REST API, you'll have to restructure your data to allow for the query you want. It is quite common in NoSQL data stores to maintain you own indexes, just for such queries.

E.g.

/users
  "12-ad-b3-ad"
    name: "Frank van Puffelen"
    stackoverflowId: 209103
    bio: "auihodasiuodsa ohdsau duia hdsauhio aoi das"
    avatarUrl: "https://www.gravatar.com/avatar/12d378e6a9788ab9c94bbafe242b82b4?s=48&d=identicon&r=PG"
  "18-a7-12-86"
    name: "Linda H"
    stackoverflowId: 3243018
    bio: "as ihuew rew i noiueh ewo we weru irew ure oew"
    avatarUrl: "https://i.stack.imgur.com/0lcm6.jpg?s=32&g=1"
/uids
  "12-ad-b3-ad": true
  "18-a7-12-86": true
/stackoverflowIds
  209103: "12-ad-b3-ad"
  3243018: "18-a7-12-86"
like image 71
Frank van Puffelen Avatar answered Oct 03 '22 10:10

Frank van Puffelen