Course

Action Lesson: Create Vote Model

In this lesson, we’re tackling an interesting modeling decision that goes beyond simply coding. Let's get into the and of implementing a voting feature in your application, where users can upvote or downvote content like questions and answers.

Let's begin with that million dollar question...

Do We Even Need a Separate Vote Model? 🤔

When designing an application, it’s essential to think carefully about and to store data, especially if it will affect core user interactions. For a voting feature, you might be wondering: Do we really need a separate Vote model, or could we simply store votes in an array within Question or Answer models?

Here are some scenarios to help you decide:

  1. Use a Separate Vote Model

    Using a separate Vote model has benefits, especially if you anticipate a large number of votes, complex filtering, or the need to track unique user interactions:

  • Handling Scale: As votes increase, querying a separate Vote model can be faster and more flexible than sifting through embedded arrays in each Question or Answer
  • Avoiding Duplication: A separate model helps manage duplicate votes (e.g., preventing a user from voting multiple times on the same item)
  • User History: This approach allows you to track individual user voting histories. You could easily fetch all votes a user has cast, regardless of whether they voted on questions or answers
  • Ease of Querying: With a dedicated Vote model, you can perform advanced queries like finding all users who upvoted a particular answer or checking if a user downvoted a specific question
Warning

Downside A separate model might add complexity, especially for small applications or cases where you don’t need fine-grained control over voting.

  1. Embed Votes in Question or Answer Models

    Alternatively, you might consider embedding votes directly in the Question or Answer models. Here’s when this option makes sense:

  • Simplicity: Embedding votes as an array within Question or Answer documents can make sense for simple apps, where you don't need complex vote tracking
  • Ease of Access: You can easily retrieve a question or answer along with its votes in a single query, reducing database calls
  • Basic Functionality: If you only need to count votes (upvotes vs. downvotes), an array of basic vote objects might be enough
Warning

Downside This approach can lead to large documents, especially if a question or answer accumulates a significant number of votes, potentially slowing down database performance.

Based on the above scenarios, make a decision about whether you want to use a separate Vote model or embed votes directly in the Question or Answer models. My advice would be to create a separate model.


Task 🎯

Now that you understand the reasoning, let’s create your Vote Model using Mongoose. This model will keep track of individual votes as separate documents, providing flexibility and scale as your app grows.

Resources 📖

As usual, you can use the following resources to help you get started

Hint 💡

Hint
monkey

Let's think about how to design it. You’ll want to ensure the model is flexible enough to track different types of votes while avoiding unnecessary complexity.

What Does a Vote Need? 🧩

A vote isn't just a "yes" or "no" action—it's part of a larger system that determines the value of questions, answers, and interactions. Here’s what your Vote model should contain:

  • Who voted: The user who cast the vote
  • What content was voted on: The question or answer being voted on
  • What type of vote it was: An upvote or downvote
  • When it was voted: For tracking voting history and behavior

What Does a Vote Model Look Like? 🧩

In order to create Vote Model, you need to define the structure of the document in a way that fits your needs. Here's how it should look:

  • Author: The user who cast the vote. This will be a reference to the User model
  • ID: The ID of the content being voted on (either a question or an answer). This should be an ObjectId referencing the relevant Question or Answer
  • Type: This tells us whether the vote is on a question or an answer. Storing this field will make it easier to query votes based on content type
  • VoteType: Whether the vote is an upvote or a downvote
  • Timestamps: Storing the creation date (using timestamps: true) can be helpful for tracking voting history and trends over time

Database design isn’t always straightforward, and choices should be based on the application’s needs and expected growth. As you keep learning, you’ll get even better at making these calls. Keep up the great work, and see you in the next lesson!

0 Comments

glass-bbok

No Comments Yet

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

tick-guideNext Lesson

Create Vote Model