0
votes

I'm building an app using flutter and firebase and was wondering what the best firestore database structure.

I want the ability for users to post messages and then search by both the content of the post and the posters username.

Does it make sense to create one collection for users with each document storing username and other info and a separate collection for the posts with each document containing the post and the username of the poster?

In the unlikely event where the number of posts exceeds a million or more, is there an additional cost of querying this kind of massive collection?

Would it make more sense to store each user's posts as a sub-collection under their user document? I believe this would require additional read operations to access each document's sub-collection. Would this be cheaper or more expensive if I end up getting a lot of traffic?

1

1 Answers

0
votes

is there an additional cost of querying this kind of massive collection?

The cost and performance of reading from Firestore are purely based on the amount of data (number of documents and their size) you retrieve, and not in any way on the number of documents in the collection.

But what is limited in Firestore is the number of writes you can do to data that is "close to each other". That intentionally vague definition means that it's typically better for write scalability to spread the data over separate subcollections, if the data naturally lends itself to that (such as in your case).

To get a great introduction to Firestore, and to data modeling trade-offs, watch Getting to know Cloud Firestore.