Get only certain keys from the collection

I would like to store an array of notes, where each note contains a title and body. Can I get a list of note titles without a body?

For instance:

notes: [ ewi4ef36wi: { title: 'Shopping List', body: 'todo' } o9kng23inw: { title: 'Things To Do', body: '' } ] 

How can I just get a list of names?

+5
source share
2 answers

Nope. The Firebase Web / JavaScript API always returns a complete tree under the nodes you requested.

The most common workaround for this is that people set up a secondary branch in the tree, where they simply store the keys.

 Notes 1: { "body": "hello", "title": "yessir" } 2: { "body": "again", "title": "title2" } 3: { "body": "there", "title": "another" } Notes_index 1: true 2: true 3: true 

This is commonly called an index. You are on('child_added' on Notes_index , and then (if necessary) get the contents of each note using once('value' .

Indexes are also often used to access nodes using an alternate key. For example, the header index for the above:

 Title_index "another": 3 "title2": 2 "yessir": 1 

This last structure may not be needed in the near future, as Firebase expands its query API to allow ordering / filtering in any field. But for your use case, the index is still useful.

+3
source

Source: https://habr.com/ru/post/1205995/


All Articles