
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 focus on creating a comprehensive question model for a database, detailing its structure and essential components. The question model is critical, containing the author's information, timestamps, number of answers, views, the question title, and the attached answers.
question.model.ts
in the database folder.title
: Required string type.content
: Required string type.tags
: Array of object IDs referencing a tag collection.views
: Number type with a default value of 0.upvotes
: Number with default value.downvotes
: Number with default value.number of answers
: Number type, not directly referencing answers.author
: Reference to a user model, also required.00:00:02 Let's develop the question model.
00:00:05 Yeah, this one right here, containing the information about the author, as well as the timestamp, the number of answers,
00:00:12 the views, the title, the question itself, and the answers attached to it.
00:00:16 It has a lot of moving parts, making it maybe the most important model in our database.
00:00:22 So let's create it by creating a new file right here in our database folder called question.model.ts.
00:00:30 To get started with it, I'll just create a copy of the account model and paste it and delete the stuff that we don't need.
00:00:39 So here I will actually remove all the types and I'll call this iModel.
00:00:46 Then here I'm going to call this model schema.
00:00:50 I'll call this iModel.
00:00:52 I'll remove the entire definition from it.
00:00:54 And here I'll also use the iModel.
00:00:57 And here we can use the model schema.
00:00:59 And finally, we can rename this to model.
00:01:03 Now what you can do is basically copy this because we'll need to reuse it a couple more times.
00:01:08 And I'll call it template.model.ts and we can paste what we have here.
00:01:14 So we can quickly get a head start on creating all of these different models.
00:01:18 In this case, we'll use a question model.
00:01:20 So what you can do is basically do a CTRL F and then search for all instances of the word model, but make sure that you match the case as well.
00:01:31 So this will only match the uppercase model words, as you can see here.
00:01:35 I think the process should be somewhat similar on other IDEs.
00:01:39 And then we can rename this with a question model.
00:01:43 Replace all instances by clicking Replace All.
00:01:46 And now we see I question, question schema, I question, question, question, question, question.
00:01:53 So I think this is good.
00:01:54 Now let's start with creating the definition of the question.
00:01:58 It's going to have a title, which is going to be of a type string, and it'll be required.
00:02:04 Next, we're going to have the content of that question, which will be of a type string and required.
00:02:10 After that, we'll have to have the tags, which will actually be an array of references.
00:02:16 So we can set the type to be equal to schema dot types dot object ID, and it's going to be a reference to a tag collection.
00:02:26 You can see that if you go to our diagram as well, where we have a question, you can see the tags is an array.
00:02:32 We're going to also have the number of views attached to this question for our recommendation algorithm.
00:02:38 So let's set it to number, default at zero.
00:02:41 Let's do the same thing for upvotes.
00:02:44 Let's also do the same thing for downvotes.
00:02:47 And let's do the same thing for the number of answers.
00:02:50 So we're only going to keep track of the number, not directly referencing the answers within the question.
00:02:56 For that, we'll create a separate collection.
00:02:59 And finally, let's create a new author, which is actually gonna be a reference to a user model.
00:03:07 And we can make it required because you cannot create a question if you don't have a user.
00:03:12 And we can also define the types right now.
00:03:14 We're gonna have a title of a type string, content of a type string, tags will be an array of object IDs, views will be a number,
00:03:23 upwards will be a number, downwards will be a number, answers will be a number, and then the author, I believe is missing,
00:03:31 will be the object ID, which is a reference to the user.
00:03:34 And that's it.
00:03:35 This is our questions model.