Firebase retrieve child keys but not values

2019-01-09 19:22发布

问题:

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?

回答1:

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"