2

I am wondering how to query only the keys of a data structure in Firebase.

I am currently doing this to search the website for the latest blogs:

const postsRef = firebase.database().ref('posts');

postsRef.once('value').then(postsObject => {
  const titles = Object.keys(postsObject.val());
  // ...
}

But it is obvious that it is inefficient as each time the user searches all of the posts, including their content, will be sent to the client when in fact I only need the titles of the posts.

For example, if this is my data structure I only need title 1 and title 2:

- posts
  - title 1    // I need this
    - ...      // I don't need this
  - title 2    // I need this
    - ...      // I don't need this
notrota
  • 1,048
  • 10
  • 21
  • There seems to be 2 unrelated questions here. I would recommend [posting them separately](https://meta.stackexchange.com/a/39224) to attract more attention and answers. This will also avoid this [question from being closed as "too broad"](https://meta.stackexchange.com/a/120634). – Grimthorr Nov 13 '17 at 11:27
  • Does this answer your question? [Firebase retrieve child keys but not values](https://stackoverflow.com/questions/32442546/firebase-retrieve-child-keys-but-not-values) – Felix K. May 20 '21 at 08:46
  • Hmm. You don't need to query for keys. You can access them directly. If you are asking how to get all of the keys within a node (posts) you need to read in the posts node and iterate over the returned snapshot to get each key. OR denormalize your data and store the keys in a separate note with a value of true. Like this `title_1: true` and then 'title_2: true` etc. Or.... there are a lot of other options but how you do it depends on your use case. And this question was from 2017. lol – Jay May 20 '21 at 20:12

0 Answers0