I have created a Firebase database with the following structure: There are 3 main collections: Users, Chats and another one which is not important for now. I have initially planned to store type documents in the Users collection, the type User is made of the properties:
uid -> string
... (some others which are not important)
displayName -> string
chatsList -> a SUBCOLLECTION of chat references, containing
chat ID
otherUserID -> the ID of the other user chatting with you
contacts -> a SUBCOLLECTION of user references, containing
user ID
user displayName
type Chat is made like this (note that the chat is only between two users):
uid -> string
user1 -> ID of one of the users in this chat
user2 -> ID of the other user
messages -> a SUBCOLLECTION of messages made of:
uid -> message ID
senderID -> user ID of who sent the message
text -> content of the message
timestamp -> when was this message sent
So if I am a logged in user I know my id and if I want my chat with user id 123 I can query the collection "users/:myID/chatsList" and find the chat reference which satisfies: "otherUserID", "==", "123" Then pick the related chat ID and find the actual chat in the Chats collection.
I am not sure but I understand that queries are shallow, so if I query for example the Chats collection I am not considering the subcollection Messages at all, so it should be faster?
Now the question is: do you think this is a good structure or is it better to not have those subcollections and use something like an array instead?