0

I want to structure a participant-list, which contains keys to other objects.

My current structure:

- events
-- 12345678910 // eventKey
--- name
--- participants // contains userIds
---- A123456789: true
---- B123456789: true

- users
-- A123456789 // userId
--- Username: 'NameA'
-- B123456789 // userId
--- Username: 'NameB'

I just save the user ids in participant list to avoid redundant data and for easier maintenance.

My question: what is the best way, to retrieve the user data of all referenced user keys? Do i have to create a query for each user id?! Or is there a better way, to save references in firebase?

Thanks in advance

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Jan Möller
  • 301
  • 4
  • 19

2 Answers2

3

To read the user data for all participants in a specific event, you will indeed have to attach a listener for each participant. This is not nearly as slow as many developers initially think, since Firebase pipelines the requests over its existing connection.

But it's also quite common to duplicate some of the data for each participant under each event, so that you can display the basic information about participants without having to read each individual user nodes.

E.g. if you display the participant names for the event, you could store the user name instead of true and save yourself the lookups. But of course that comes at the cost of then having duplicated data, and having to figure out whether/how to keep that up to date. To read more about basic strategies for that, see

Some further good reading on the topic:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
2

With your current structure, you have to perform multiple queries.

It is very common in NoSQL databases to duplicate data in order to avoid having to perform multiple queries like this.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441