Prabodh
Prabodh

Reputation: 175

Firestore: which of the following data models would be more efficient

I am making a public chat app using react and firestore and I am storing chats using the following model:

In firebase real-time database
userid:
      username: username
      status: (online | offline)
In firestore
rooms:
      roomid:
             roomOwner: username
             participants: [username]
messages: 
      messageID:
             roomid: roomid
             from: username
             createdAt: server timestamp

So when querying the data I was thinking of doing something like this:

const query = messagesRef.where('roomid', ==, "USERS_ROOMID").limit(25);

So should I do this or modify rooms to:

rooms:
     roomid:
           owner: username
           participants: [username]
           messages: //Create new collection
                   messsageId:
                              createdAt: server timestamp
                              from: username

Upvotes: 0

Views: 87

Answers (1)

Gourav B
Gourav B

Reputation: 972

So, I guess the question here is, to choose between Root-level collections(1st option) and Subcollections (2nd option).

Root-level collections(1st option) are generally good for many-to-many relationship scenarios. As rooms and messages don't have a many-to-many relationships, it's better avoided.

For the current scenario, 2nd option is a better choice. It gives the benefit of size and still gives good query capabilities.

Please refer to the below Firestore official documentation for reference: https://firebase.google.com/docs/firestore/manage-data/structure-data

Upvotes: 1

Related Questions