Is there something similar you can do with firestore? I haven’t been able to find anything about that.
I have a users collection in firestore, which holds information such as the users name, image URL and id. I also have a messages collection, which stores all the messages posted by users. The message has a uid field that points to the id of the user that posted the message. Is there a good way to get the user’s information from that id? I really don’t want to have to use something like the code below to do it.
I suppose you would have some kind of conversation model or some way to group them, no? If so, then you query that first (the participants), and then query the messages for each one.
If you, on the other hand, need to retrieve all the messages for every user, then there’s no other way.
Would it be better to store the user data in the message as it is created instead of just the user id? Btw I’m using firebase auth, but the users collection is just so that their public data their data is accessible to other users.
I think that is safe to be fair, but it depends on the nature of the application.
Heavily accessed? How big these documents are? What is the latency impact of a given query.
I guess you will generate some indexes to help with that when is necessary.
99% you should be fine to have some user data on the message, if not bothered of duplicates.
Why don’t you have a sub collection per user and each user has his messages. Would that not work better in your case?
Sorry, but funny enough your use case is an example in the docs once again.
“The best way to store messages in this scenario is by using subcollections. A subcollection is a collection associated with a specific document.”
@skaparate and @GeorgeCrisan thanks so much for your help! After doing some research, I found out that mongoose actually has to query for each document that it references by id as well, so it looks like the way I originally had it is the way to go. But I really appreciate your feedback, it definitely gave me some things to think about.