0

Is it better to store lets say users in an array or just objects. I wanted to create unique keys, but i am seeing that arrays is going to be a hassle to do.

My other question was that is it better to keep a list of users for a specific room inside that room or in another object outside the room that links to it?

Bk Razor
  • 1,492
  • 1
  • 14
  • 24
  • 1
    Go through Firebase documentation, learn from online tutorials. Because, I understand you are completely new to Firebase. Firebase is a NOSql database not RDBMS. Things work a lot different here. No tables, no primary keys, foreign keys, no arrays. It's all just objects. I started from here https://www.youtube.com/playlist?list=PLGCjwl1RrtcTXrWuRTa59RyRmQ4OedWrt – Nishanth Sreedhara Dec 11 '16 at 13:32
  • I'm relatively new, but not completely new. There actually are keys and there actually are arrays in firebase – Bk Razor Dec 11 '16 at 14:57
  • I've been using both until now, but arrays don't seem to be the way to go as seen by the answer below. – Bk Razor Dec 11 '16 at 14:58

1 Answers1

2

Please don't use arrays unless you really need to. Their use is situational at best and there are usually way better ways to store your data. See the answer to this Swift question, it may help a bit

Retrieving an array

Array's in Firebase are not like an array in code. There is no insert, update or remove. There is no access to an individual element for those functions - if there is a change, the entire array has to be re-written so it's use is limited.

For the second part of the question: A typical design pattern for Firebase is to leverage a /users node and keep references to the users id (uid) in other nodes.

users
  firebase_uid_0
    name: "Biff"
    email: "biff@thing.com"
  firebase_uid_1
    name: "Leroy"
    email: "leroy@ubrs.com"

This structure and use is covered in the Firebase documentation as well as numerous times here on stackoverflow.

so a possible structure

rooms
  room_0
   users
     firebase_uid_0: true
  room_1
   users
     firebase_uid_1: true
  room_2
   users
     firebase_uid_0: true
     firebase_uid_1: true

and

room_0 has uid_0 in it but not uid_1
room_1 has uid_1 in it but not uid_0
room_2 has both users in it

That being said, the answer is really dependent on your use case. If the structure is any deeper than my suggested one, then you may want to denormalize the data. If you need to run queries on a deeper level, you may also want to consider an alternate structure.

Community
  • 1
  • 1
Jay
  • 34,438
  • 18
  • 52
  • 81