1

I am an experienced developer but a beginner in database management. I am wondering what is the best pattern to organize my database stored in Firestore (NoSQL).

I have two patterns in mind:

  • a user has pictures. I want to be able to get all the pictures for one user, and to get the picture owner easily
  • a chat room will have users, and users will have several chat rooms. It want to query a user's list of chat rooms and a chat room list of users. I want to be able to delete my chat room and its content if the last user leaves it.

What is the most efficient pattern to manage this in a Firestore context? Should I use backrefs or cross reference the documents, or should I use a query for all the images / chat rooms where the userId matches my user? How do I manage efficiently the "garbage collection" use case?

I have started with cross references but I am concerned by the extra cost of writing and editing more data (a user enters a chat room, it edits the user and the chat room), or the latency increase for the "get all pictures" use case if I store a lot of pictures and I rely only on indexes.

  • Did you see https://stackoverflow.com/questions/46625654/firebase-cloud-firestore-many-to-many-relationships, https://stackoverflow.com/questions/72995390/how-can-i-model-a-many-to-many-relationship-in-firestore-without-exceeding-the-d, and more from https://www.google.com/search?q=firestore+many+to+many? – Frank van Puffelen Mar 19 '23 at 19:40
  • @FrankvanPuffelen I was missing the "many-to-many" keyword. Thank you! – Carl Sarkis Mar 19 '23 at 21:26

0 Answers0