Open zsoltime opened 6 years ago
What about to use author's id to fetch his/her information in questions and answers? So we'll have an authors collection and posts collection which will contain authors id's. If we follow it, we will not write a new document for users who are already posted something.
Well it might be looking too far in the future but what about extra resources that we were thinking about. Will we just be having text answers or supporting content?
Is this a better format than say having a separate collection just for answers, and then linking them by questionId. But I'd be happy to have a schema like this. Do we need total votes so we can track top contributors or is that also something for a later date?
@Heyjp I hope that's not too far :) I think we could stick to text content (with markdown support, maybe?) first.
I'm also thinking about adding an answers
collection now but I'd still keep the author's id
, name
and avatar
to reduce the number of queries. I'll open another PR where we'll have this answers
collection.
I'm not sure how we should store the votes as we need to track who voted already.
@OStefani MongoDB is designed to store denormalised data and doesn't support JOIN
s like SQL does. If you need data from more than one collection, you need to perform more queries. Embedding the most frequently read user data fields in the questions
and answers
collections will save us extra queries.
How will the database schema look like? We're using Mongo, so we can embed documents (like the
author
in thequestions
collection) to reduce the number of queries.I think the documents below are good starting points.
Users
I think the user document is pretty self-explanatory. Maybe we could also add
top_questions
andtop_answers
fields with an array of 2-3 questions/answers to fetch everything in one single query and display these on the user's profile.Questions
The questions documents are a bit more complex. I embedded the author, so we can fetch their name and avatar in one query. Not sure how the profile links will look like, but we can start with
/profile/{user-mongo-id}
and add slugs later (/profile/{user-shortid}
). In that case we'll add aslug
field :)Answers are also embedded at the moment. We could also just create an
answers
collection with all the answers and only embed the top or latest answers to each question, but not sure if it has any performance gains :)