Retrieve only specific keys from collection

前端 未结 2 712
迷失自我
迷失自我 2020-12-20 18:55

I would like to store an array of notes, where each note contains a title and a body. Is it possible to retrieve the list of note titles without the body?

For exampl

相关标签:
2条回答
  • 2020-12-20 19:25

    Nope. Firebase's Web/JavaScript API always returns the full tree under the nodes that you request.

    The most common workaround for this is that people set up a secondary branch in the tree where they just keep 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 referred to as an index. You'd on('child_added' on Notes_index and then (if needed) get the content of each note using once('value'.

    Indexes are also often used to make nodes accessible by an alternative key. Such as a title index for the above:

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

    This last structure might soon not be needed anymore, since Firebase is extending their query API to allow ordering/filtering on any field. But for your use-case an index is still useful.

    0 讨论(0)
  • 2020-12-20 19:28

    Absolutely, when you set the title or body values for the note be sure to set a priority. You can then bind to the notes endpoint (receive events and data) limited to keys matching a specified priority.

    0 讨论(0)
提交回复
热议问题