
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
Complete source code available till this point of lesson is available at
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, we explore the creation of a vote model to efficiently manage voting on questions and answers. This model is designed to avoid large arrays of user IDs by maintaining a separate collection for votes, which references the user and the post being voted on.
00:00:02 Let's create a vote model for storing votes for questions and answers.
00:00:07 You see, it's better to have a separate collection for the vote rather than having a whole array of different users to indicate the number of votes.
00:00:16 What do I mean by that?
00:00:17 Well, if you take a look at the vote object, you'll notice that it has to contain the object ID, which is a reference to the author.
00:00:25 of that vote, meaning which user upvoted it, and then it has to contain a reference of on which answer or question or post,
00:00:34 whatever it is, we're trying to place an upvote or a downvote on, which is denoted by the vote type.
00:00:41 So instead of handling this on the question or the answer level, where you could say that upvotes is an array of the user IDs that upvote that post,
00:00:50 which would result in a huge array of a lot of users with a lot of references, Here on the question, we actually just store the number of upvotes.
00:00:58 And then here we actually keep track of which user liked or disliked what.
00:01:03 So let's create it by creating a new database model of vote.model.ts.
00:01:10 Let's copy over the template and let's change the model to vote.
00:01:18 And then we can start defining its definition.
00:01:21 Each vote has to have an author of the vote, meaning who voted what.
00:01:26 And this will be a schema dot types dot object ID with a reference of user and required to true.
00:01:35 After that, we have to have the ID of the post, which is going to be a reference to either a question or an answer.
00:01:42 So let's say ID is a type schema.types.objectId required to true.
00:01:49 Next, we can have a type.
00:01:50 So this is very important to know, are we voting on a question or on an answer?
00:01:56 So the type will be a string, but we'll actually provide an enum of either the question or the answer.
00:02:07 So this means that the string can be only one of those two things, question or the answer.
00:02:13 And finally, the vote type.
00:02:15 So we can define the vote type, which will be equal to a type string with an enum of upvote or downvote and required will be set to true.
00:02:26 Now we can define this type by saying author will be of a type's object ID.
00:02:32 ID will be of the type's object ID.
00:02:35 Type will be a string, but you can also define it like this, question or answer.
00:02:39 And vote type will be a string of either upvote or downvote.
00:02:44 And that's it.
00:02:45 That's our vote model.