0

I am actually new in programming and in Firebase. I need to know which one is better between these 2 scenarios.

I am making an event app. and if a user will attend the event, I write a document in the subcollection "attendedEvents" that nested in "users" collection like this:

enter image description here

and then, I need to manage and to display the full data of that attended event to client app, i need to display the data like location, coordinate, venue etc etc.

there are two ways to save the attended events data

1. I just write eventID in the "attendedEvents" subcollection. like this

enter image description here

and then when i need to display the full data, i need to do 2 steps. first I get all the eventID in that "attendedEvents" subcollection, and then do a query specifically based on eventID to get the full event data.

2. I write all the events data in the the "attendedEvents" subcollection fields, not just eventID. like this

enter image description here

so when i need to display the event data in the client app, I just need to do one query only to reference "attendedEvents" subcollection.

what is the advantage and the disadvantage between these two?

to be honest I am not really sure to choose. but at the moment I will choose just to write the eventID, since there is storage pricing in firebase, even though the data real time synchronization is not really needed when i display that attended event data to client app.

and how about Network bandwidth ? since You are charged for the network bandwidth used by your Cloud Firestore requests since the first choice takes more bandwidth right? I am not really grasp about this network bandwidth

sarah
  • 3,819
  • 4
  • 38
  • 80

1 Answers1

1

Both are valid approaches, and neither of them is pertinently better than the other. It all depends on what your use-cases are, and how comfortable you are with duplicating data.

In your first approach you write very little duplicated data (pretty much just the ID). So that means that your code for writing this data is going to be quite simple and quite fast. But when reading the data, you will need to load the data from both collections. This typically isn't a big performance concern for reasonable numbers of documents, but definitely does require more code and more API calls.

In your second approach you duplicate all data for an event for each attendedEvents document. This means that the code to write this data is more complex, and you're definitely storing more data. And you'll need to figure out if and how to keep it up to date (see my answer here for more on this). But on the other hand, reading an attendedEvent document now gives you all information about the events document in one read.

This is a common consideration in NoSQL databases: you'll often have to consider write performance and disk storage vs read performance and scalability. As I started with: there's no answer that is best in all cases. It all depends on what the most important criteria for your app are.

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