
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 how to create a model for tags in a database that effectively categorizes questions. The discussion centers on setting up two different models: one for tags and another for tag questions, optimizing the querying process for efficient data retrieval.
00:00:02 Let's create a model for our tags.
00:00:05 Tag is a collection that helps us categorize different questions.
00:00:09 That means that each tag can contain multiple questions, but we also want to be able to filter through tags themselves, not caring about the questions
00:00:17 within them, like in this page right here.
00:00:19 So in this case, we'll do a great optimization for querying your database.
00:00:24 If you take a look at the database structure, you can notice that we're going to have two different tag models.
00:00:30 One will be for the tag itself, which simply has its name, the number of questions it has, and then we'll have a separate one where we can pull all the
00:00:41 questions related to that tag.
00:00:43 I mean, now that I mention it, it makes complete sense because you have to ask yourself in which scenario will you have to query that collection.
00:00:51 We're querying the tags in the tags page where you want to see all the different tags But you don't necessarily need to see 23,000 questions within them.
00:01:01 You just want to see this single number.
00:01:03 So this is a perfect way to do that.
00:01:05 You can simply query the tags collection to see their names and the number of questions.
00:01:10 But then, when you want to extract the actual questions belonging to that tag, you can use the second model.
00:01:16 And I'm using the terms model and collection and even schema interchangeably.
00:01:21 because they're all pointing to the same thing, a collection or a model off of which we can create a document or an instance of that model.
00:01:30 Hopefully that makes sense.
00:01:32 So let's go ahead and create a new file called tag.model.ts.
00:01:38 Let's start from the template model and I will once again rename it to simply say tag.
00:01:46 There we go.
00:01:47 Make sure to do the same.
00:01:48 And if you're not doing these quick tricks, as I am doing with auto change of the specific text, then be very careful for all of the things to have changed.
00:01:58 The tag, the tag schema, the types and more, because if you're not careful, that can cause issues.
00:02:05 Oh, such as the issue that I have caused right here.
00:02:08 Check this out.
00:02:09 It didn't actually change the account because I forgot to change that from the last time.
00:02:14 So thankfully I have caught myself in the error.
00:02:17 Let's actually go over these last couple of models to make sure that we don't have an issue.
00:02:23 Here, I changed it to account.
00:02:25 That's good.
00:02:27 in the answer.
00:02:28 Oh, it says account.
00:02:29 This should say answer right here.
00:02:31 So please be careful that all of these properties are properly typed out.
00:02:36 I'm actually glad the error happened to me.
00:02:38 So you can know that errors do happen, but this is going to cause significant issues later on in our database if we don't fix them on time.
00:02:46 And for the question, it says account, it needs to say question.
00:02:52 Oh, take a look at this.
00:02:53 It did not auto change this.
00:02:55 This should also say question.
00:02:58 Okay.
00:02:58 I think we're good now.
00:03:00 No other instances of answer.
00:03:03 What about the answer itself?
00:03:06 It also says models.account.
00:03:08 It should say answer.
00:03:09 So once again, please be careful.
00:03:11 I know that I'm trying to save some time for all of us, but as you can see, it can cause further issues.
00:03:17 So I'll change this over to tag and I'll change this over to tag as well.
00:03:23 Great.
00:03:23 Now let's create its definition.
00:03:25 The tag will be pretty simple.
00:03:27 It'll simply have a name, which will be a type string and it'll be required to true.
00:03:34 And each string also has to be unique.
00:03:37 So we can say unique is set to true.
00:03:40 Next, we can have the number of questions.
00:03:42 So this will simply be questions.
00:03:45 And it'll have a type is equal to number and a default of zero.
00:03:53 And did you see how chat GPT try to turn this into a collection of references?
00:03:58 Because it doesn't know the optimization we're trying to implement.
00:04:01 So it's good that we actually have it like this.
00:04:04 And it's good that you're watching this course.
00:04:05 After all, AI cannot do database architecture for you, at least not yet.
00:04:10 So let's give it a name of string and let's give it questions equal to number.
00:04:18 And in this case, we're not using types, so we can remove that.
00:04:21 Great.
00:04:21 So now we have our tag model.
00:04:23 And let's create the second one, which is going to be the tag question model.
00:04:28 I'll do that by creating a new file called tag-question.model.ts.
00:04:34 And we can copy the template within it.
00:04:36 But now I noticed why I have those issues.
00:04:38 I forgot to change those two keywords in the template itself.
00:04:42 So this should say model.
00:04:44 I'll copy the template and I'll paste it here.
00:04:48 I'll replace the model with tag question.
00:04:52 So this is now a joined collection.
00:04:54 And let's define the definition.
00:04:56 Hopefully it'll make more sense then.
00:04:58 So a tag question schema will contain a reference to a tag.
00:05:03 So it'll be a type object ID, ref tag, and it'll actually be required.
00:05:08 So we can say required.
00:05:10 is set to true.
00:05:11 And after that, we can get the question attached to that tag, which will be equal to type, object ID, with a reference to a question,
00:05:19 and required to true.
00:05:21 And see what Copilot did?
00:05:22 Once again, it took types.objectId, which are TypeScript types and not schema types.
00:05:28 So this is actually schema.types.objectId.
00:05:33 And let's actually define the types by giving it a tag of types dot object ID.
00:05:40 And let's also give it a question of a types object ID.
00:05:44 And with that, we have created those two different tag and tag questions models.
00:05:49 Hopefully the reasoning of why did we decide to create two separate collection makes sense.
00:05:53 The quick answer to that question is for easier and faster querying.
00:05:58 Whenever we need to fetch all the tags like this, we just get their names and the number of questions they have.
00:06:04 And when we need to find which tags are attached to each question like this, then we can use the tag question collection.
00:06:11 Great.