0

I list the car brands as follows. I keep the cars the user likes in a separate table. How can I list the cars that the user likes with their features? I am using firebase realtime database.

{
cars:{
  {"id" : 1, "title" : "BMW"},
  {"id" : 2, "title" : "Toyota"}
  },
  likes:{
    "1":{user1: true, user2: true},
    "2":{user1: true},
  }
}

How should my table JSON layout be?

I studied the Many to Many relationship in Firebase question, but I could not understand how to get the car information.

Gurkan T
  • 198
  • 2
  • 14
  • This is a fairly traditional many-to-many relationship, for which I recommend creating as many "connector tables" as you need. See https://stackoverflow.com/q/41527058. With that you can then load the data pretty easily. If you're struggling with that part, please edit your question to [show what you've already tried](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jan 14 '21 at 17:59
  • I studied that question but could not understand.@FrankvanPuffelen – Gurkan T Jan 14 '21 at 18:46
  • It is really hard to help with "I could not understand", as that's the best explanation of the concepts that I know off. Hence the request to show what you've already tried in code. – Frank van Puffelen Jan 14 '21 at 18:52

1 Answers1

0

Add a features tree, or table as you call it, so as to avoid nesting data. In terms of recording a users feature preferences that would be yet another JSON tree, or table, of data.

{
cars:{
  {"id" : 1, "title" : "BMW"},
  {"id" : 2, "title" : "Toyota"}
  },
  likes:{
    "1":{uid1: true, uid2: true},
    "2":{uid1: true},
  },
  features:{
    "f1":{sunroof: true, bluetooth: true},
    "f2":{offroad: true},
  },
  preferences:{
    "uid1":{f1: true, f2: true},
    "uid2":{f1: true},
  }
}
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91