Course

Action Lesson: Create Question Model

You've built a lot so far, but now it's time to dive deep into the heart of your Stack Overflow clone—the Questions.

What Does a Question Need? 🤔

Before you start coding, ask yourself: what exactly does a question contain?

What will make it tick? What information will you need to store so it can grow, evolve, and engage your users? 🧐

A question is more than just a string of text. It’s a , in your application. But what exactly defines it? Here’s what you might wonder as you design it:

  • Does it need a title?

    Of course! But what type of title will it be? Should it be a simple string or perhaps something more complex?

  • What about the content?

    This one is tricky. How do you store rich, detailed content? Will it be plain text, or will you support Markdown or HTML?

  • What metrics will matter?

  • Views — how many eyes have seen this question? What type of number do you need for that?
  • Answers — how many responses has this question gathered? Should we count these as a number or track more complex metadata?
  • Votes — is a question good? Should it be upvoted or downvoted? And how do we represent the vote count?
  • How will you store the author's identity?

    Will each question be linked to a specific user? How do we represent this user’s unique identity in our model?

Question yourself to get your Question Model in shape. Let's get started!

Task 🎯

Your mission is to Create a Question Model using Mongoose.

Resources 📖

To help you with this task, here are some valuable resources:

Be sure to check these out for a deeper understanding! 📚

Hint 💡

Hint
monkey

First, choose what you want to display for each question on the UI by cross referencing it with provided Figma design. Here are some ideas:

  • Title: How will the user recognize the question?
  • Details: What is the question about? (A good, detailed description will help.)
  • Author: Who asked this question?
  • Creation Date: When was this question created?
  • Answers: How many answers has this question received?
  • Tags: What topics or categories does this question fall under?
  • Votes: How many upvotes or downvotes does this question have?
  • Views: How popular is this question? (How many people have viewed it?)

Is this everything? Possibly! But it’s worth double-checking in case something important is missing. 😉

Let’s Think About Data Types 🧠

Now that you know what to include, think about how you’ll store each piece of data.

  • Title: Likely a String, but consider any length restrictions
  • Details: Another String field, but this one might require a longer limit
  • Creation Date: What's a suitable way to store dates? (Hint: consider timestamps or date types.)
  • Author: How will you link this question to a specific user?
  • Answers: Do you want a simple number (total answers) or a list of all answers? Think about which will make queries faster
  • Tags: Do you want tags to be just words, or do you need a way to connect each tag to other questions?
  • Votes (Upvotes & Downvotes): Would separate fields work here, or could you store them in a different way?
  • Views: How will you track the number of views? A counter might do the trick

What About Relationships? 🤔

Some fields—like the author and tags—are connected to other entities. Let’s think through them:

  • Author

    Instead of storing all the author’s details here, how about just storing a reference (like their ID) to a separate User model? This way, if the author updates their information (like a name change), it updates everywhere.

  • Tags

    A question can have multiple tags, and tags can connect to multiple questions. How might you represent this many-to-many relationship? Should each question store tags directly, or should it refer to a Tag model?

  • Answers

    Consider whether you want to store answers in the Question model or keep answers in a separate model with a relationship to the Question. (Hint: a separate model makes sense if you expect many answers per question.)

  • Votes

    For votes, you’ll likely want separate fields to keep track of upvotes and downvotes. But consider whether each vote will be linked to specific users (e.g., to prevent duplicate voting).

Final Thoughts ✨

With each field, ask yourself:

  • Is this field a simple piece of data (like a title or view count), or is it connected to another model (like an author or tags)?
  • How does this field affect the data structure? For instance, relationships (like with users or tags) can simplify updates but may add complexity to queries

And remember, designing a model is about balancing with the to grow as your app does. Keep iterating, and use the Mongoose docs if you need guidance on setting up relationships!


As they say, “Success is the sum of small efforts, repeated day in and day out.”. Keep trying!

Loading...

0 Comments

glass-bbok

No Comments Yet

Be the first to share your thoughts and start the conversation.

tick-guideNext Lesson

Create Question Model