0

I'm trying to put all different possible values of a key in different Firebase folders into an array.

E.g all possible values for "favourite_ice_cream_flavour"

projects
    randstring123
        favourite_ice_cream_flavour: chocolate
        otherinfo: n.i
    randstring345
        favourite_ice_cream_flavour: cranberry
        otherinfo: N.I
    randstring789
        favourite_ice_cream_flavour: chocolate
        otherinfo: NI

With these values, the array concerning "favourite_ice_cream_flavour" should return

["chocolate", "cranberry"]

Note: chocolate only appears once.

What would be the most efficient way to go about this?

Louis Vetter
  • 199
  • 2
  • 10

1 Answers1

1

The most efficient way would probably be to modify the data model of your Real Time Database in order to add an extra node where you list all the favourite_ice_cream_flavourS

Something like that:

projects
    randstring123
        favourite_ice_cream_flavour: chocolate
        otherinfo: n.i
    randstring345
        favourite_ice_cream_flavour: cranberry
        otherinfo: N.I
flavours
    chocolate: true
    cranberry: true
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • 1
    Upvoted because this is indeed the most efficient way to solve this. In fact, the question often stems from wanting to show a list of categories to the user first, in which case it's a form of [categorization problem](http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value). – Frank van Puffelen Feb 25 '18 at 16:19