Firebase and select distinct

前端 未结 2 1120
南笙
南笙 2020-12-22 11:26

My data in firebase looks like

Can I do a select distinct on the Genre node using the REST API of firebase?

相关标签:
2条回答
  • 2020-12-22 11:33

    When you read a tree in Firebase, you get all the children. There's no way around this.

    You can however, create a data structure that holds just the genres per movie.

    {
       "movieGenres": {
          "3646": {
            "0": "Comedy"
          },
          "3647": {
            "0": "Drama"
          },
          "3648": {
            "0": "Horror",
            "1": "Sci-Fi"
          }
       }
    }
    

    Now getting the genres is a simple read.

    0 讨论(0)
  • 2020-12-22 11:41

    David's answer is correct (would you expect anything less?)

    I wanted to expound his answer a tad with a more concrete example:

    Structure

    movies
      movie_id_0
        name: "Star Trek: Beyond Thunderdome"
        genre: genre_id_0
      movie_id_1
        name: "Airplane: Part Trois"
        genre: genre_id_1
      movie_id_2
        name: "Star Wars: Episode 132"
        genre: genre_id_0
    genres
      genre_id_0
        description: "Sci-Fi"
        verbose: "Science Fiction is the literature of change"
      genre_id_1
        description: "Comedy"
        verbose: "Comedy is entertainment consisting of jokes and satirical sketches"
    

    There are several advantages of this structure in that the actual data is disassociated from the node names so if you wanted to say, change Sci-Fi to Science Fiction, you can make one change and everything that references it will reference the newly updated name. Your structure reflects this technique.

    You can also see that we can add more info, verbose, about what a Sci-Fi or Comedy movie is

    And this also easily allows you to maintain, read and write your genre list with minimal effort.

    Hope that helps!

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