Taha Sami
Taha Sami

Reputation: 1697

How to build data structure correctly on firestore

I have quiz game and I want to add 1000 questions on firebase firestore and I have thousands of users in my game and every user has special id.

What is the best way to know if the user answered this question before or no?

Should I add user id inside every question who answered it or there is the another way?

I want build data structure logically for reduce consumptionmy read limit on firestore.

Upvotes: 1

Views: 94

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

The two most common solutions I can quickly think of:

  1. Create a single top-level collection answers, where you store all answers for all users. In that collection use the combination of UID and question ID to name each document, so that you can check if a document /answers/$uid+$questionID exists.
  2. Create a top-level collection users in which each UID represents a user. Under each UID document create sub-collection answers, where you use the question ID to name the documents. Now you can check for each user if they answered a specific question, by checking /users/$uid/answers/$questionId.

Neither of these is pertinently better than the other, and they typically allow for similar use-cases. I'd usually pick the second structure simply because it results in a more natural distribution of the data, but it's s pretty small preference in practice.

Upvotes: 2

Related Questions