1 00:00:00,740 --> 00:00:05,360 In the last section we did a quick review on what we're trying to accomplish right now at the very end 2 00:00:05,390 --> 00:00:11,180 we had said that we need to create a new model class using mongoose which will allow us to create a 3 00:00:11,180 --> 00:00:14,810 new collection of records inside of our Mongo database. 4 00:00:14,810 --> 00:00:21,790 So right now we want to make this model class which represents a huge collection of records. 5 00:00:22,160 --> 00:00:25,910 Now to do so I'm going to flip back over to my code editor. 6 00:00:25,910 --> 00:00:29,870 We need to find some suitable location to create this model class. 7 00:00:29,870 --> 00:00:35,270 Now I don't think it really belongs inside of our index yes file because this is really associated with 8 00:00:35,270 --> 00:00:38,330 kind of booting up or starting up our application. 9 00:00:38,330 --> 00:00:42,410 So instead I think that I'm going to make a new directory inside of my project. 10 00:00:42,410 --> 00:00:45,950 So a new folder I'm going to call it models. 11 00:00:46,100 --> 00:00:52,040 So this is going to contain all the different models or model classes that we create using mongoose 12 00:00:52,760 --> 00:00:55,330 inside of this file or Symeon sided folder. 13 00:00:55,370 --> 00:00:56,600 We'll make a new file. 14 00:00:56,800 --> 00:01:00,560 I'm going to call it user dot jaywalks. 15 00:01:01,480 --> 00:01:01,840 OK. 16 00:01:01,910 --> 00:01:11,030 So inside of here we will create our mongoose model class to do so at the very top we will require in 17 00:01:11,150 --> 00:01:19,900 the mongoose library like so and then we're going to pull one property off of this mongoose object we'll 18 00:01:19,910 --> 00:01:24,920 say Konst schema equals mongoose dot scheme. 19 00:01:25,400 --> 00:01:31,580 Now you'll notice that the value or the property name schema right here is identical to the variable 20 00:01:31,580 --> 00:01:33,290 name that we are assigning it to. 21 00:01:33,290 --> 00:01:38,800 So we can use a little bit of yes 20:15 destructuring just to make sure we're on the same page here. 22 00:01:38,810 --> 00:01:42,870 Just one time we're going to talk a little bit about destructuring very quickly. 23 00:01:42,980 --> 00:01:52,220 So the code that we wrote right here can be condensed down to just curly braces schema equals mongoose 24 00:01:52,400 --> 00:01:58,450 like the so line to right here and line three 100 percent completely equal. 25 00:01:58,730 --> 00:02:03,510 This line right here says the mongoose object has a property called schema. 26 00:02:03,770 --> 00:02:07,700 Take that property and assign it to a new variable called schema. 27 00:02:07,700 --> 00:02:09,930 That's what these curly braces indicate. 28 00:02:10,130 --> 00:02:13,000 We're going to be using destructuring like this throughout this course. 29 00:02:13,040 --> 00:02:14,390 A tremendous amount. 30 00:02:14,630 --> 00:02:19,590 And at every point in the future I will do a very brief mention of the fact that we were using the structuring 31 00:02:19,790 --> 00:02:25,210 but I will probably not go through the entire process of saying oh these two things are equivalent again. 32 00:02:26,050 --> 00:02:26,560 OK. 33 00:02:27,450 --> 00:02:28,410 Oops. 34 00:02:28,900 --> 00:02:29,140 OK. 35 00:02:29,140 --> 00:02:32,970 So we've got the mongoose library and we've got this schema thing right here. 36 00:02:33,010 --> 00:02:37,540 Now what in the world are we even use in schema for right like what does this for. 37 00:02:37,870 --> 00:02:40,440 Well let's make sure it's really clear. 38 00:02:41,050 --> 00:02:46,420 Just a moment ago when we started talking about Mongo in general we had said that a collection has many 39 00:02:46,420 --> 00:02:47,640 records right. 40 00:02:47,650 --> 00:02:54,220 We spoke about that and we had also said that every record inside of a collection can have its own unique 41 00:02:54,220 --> 00:02:55,630 set of properties. 42 00:02:55,630 --> 00:02:59,970 So for example this first record right here has a height property. 43 00:03:00,190 --> 00:03:02,350 But the second one does not. 44 00:03:02,380 --> 00:03:06,850 This record has an age property but no other record does. 45 00:03:06,850 --> 00:03:13,840 So we had said that with Mago we can have as many random arbitrary properties as you could possibly 46 00:03:13,840 --> 00:03:17,770 imagine on any different record how ever. 47 00:03:17,770 --> 00:03:24,400 However when we make use of mongoose it kind of curtails or kind of removes that ability. 48 00:03:24,400 --> 00:03:30,650 Mongoose wants to know all of the different properties that our records will have inside of our database. 49 00:03:30,730 --> 00:03:35,610 And it requires us to define all those ahead of time with this schema object. 50 00:03:35,650 --> 00:03:41,230 So when we make use of mongoose we kind of lose out on the ability to have just a bunch of random different 51 00:03:41,230 --> 00:03:46,900 properties on each individual record because mongoose wants to know ahead of time all the different 52 00:03:46,900 --> 00:03:50,040 properties that we might have. 53 00:03:50,110 --> 00:03:55,330 So right underneath our two requires statements or one require statement I should say we are going to 54 00:03:55,330 --> 00:04:00,660 use this schema object to create a schema for this new collection. 55 00:04:00,880 --> 00:04:05,980 The schema will describe what every individual property personally every individual record is going 56 00:04:05,980 --> 00:04:07,250 to look like. 57 00:04:07,330 --> 00:04:12,030 So I'm going to say Konst user schema equals new schema. 58 00:04:12,460 --> 00:04:15,070 And then we're going to pass it an object. 59 00:04:15,250 --> 00:04:19,630 This object right here is going to describe all the different properties we have. 60 00:04:19,630 --> 00:04:25,960 So right now I can really only think of like maybe one very distinct property that I know we're going 61 00:04:25,960 --> 00:04:31,700 to have and that is our users Google ID or the ID out of their profile. 62 00:04:31,870 --> 00:04:38,200 So I'm going to say that every user inside of our users collection will have a property called google 63 00:04:39,070 --> 00:04:40,770 id like so. 64 00:04:40,930 --> 00:04:45,920 And that is going to be of type string. 65 00:04:45,960 --> 00:04:48,600 Now two things I want to point out in great detail. 66 00:04:48,600 --> 00:04:50,960 Notice that I have a lower case D here. 67 00:04:51,030 --> 00:04:52,450 You can use a capital D. 68 00:04:52,560 --> 00:04:55,540 You just have to be consistent everywhere throughout your application. 69 00:04:55,560 --> 00:05:01,710 I'm going to use the lower case like so now the second thing is the value that we assign to this string 70 00:05:02,430 --> 00:05:09,360 that tells our schema that any time there is a value on this google id property it will always be a 71 00:05:09,360 --> 00:05:12,150 string and it should treat it like a string. 72 00:05:12,180 --> 00:05:15,900 If this was instead a number we would say this is a number. 73 00:05:16,240 --> 00:05:19,290 In our case the Google ID is a string. 74 00:05:19,290 --> 00:05:25,150 Now if you remember looking at the Google ID inside of our profile when we cancel logged it much earlier. 75 00:05:25,170 --> 00:05:25,850 Let's see. 76 00:05:26,130 --> 00:05:30,540 I don't have it up anymore but when we have previously looked at our Google ID you might have noticed 77 00:05:30,540 --> 00:05:33,410 that it was only a sequence of numbers. 78 00:05:33,450 --> 00:05:36,240 However those numbers were wrapped inside of a string. 79 00:05:36,240 --> 00:05:40,750 So we're just going to call the thing a string and call it a day OK. 80 00:05:40,770 --> 00:05:46,650 So at this point we have created a schema that describes what a user will look like and we're only going 81 00:05:46,650 --> 00:05:49,870 to care about the google id property for now. 82 00:05:49,890 --> 00:05:57,000 Now if you create an actual model class and tell mongoose that it needs to be aware that this new collection 83 00:05:57,090 --> 00:06:03,240 needs to be created we are going to add one line of code at the bottom which will say mongoose model 84 00:06:04,800 --> 00:06:10,080 the very first argument will be the name of the collection which is going to be users because it's our 85 00:06:10,080 --> 00:06:14,610 users collection it's a collection of all the different users inside of our application. 86 00:06:14,610 --> 00:06:22,170 And then the second argument will be that user schema that we just created so will say user schema like 87 00:06:22,170 --> 00:06:23,300 so. 88 00:06:23,920 --> 00:06:25,390 OK that's pretty much it. 89 00:06:25,680 --> 00:06:32,310 So by using this mongoose model command we are telling mongoose that we want to create a new collection 90 00:06:32,490 --> 00:06:34,140 called users. 91 00:06:34,260 --> 00:06:39,530 Now when Mungo's boots up if this collection already exists it's not going to delete it and remake it. 92 00:06:39,570 --> 00:06:45,360 It's just going to say oh OK it looks like there's already a collection of users in the database and 93 00:06:45,390 --> 00:06:47,950 I've got some scheme for something called the users. 94 00:06:47,970 --> 00:06:52,590 So these two things might match up smuggest will not overwrite existing collections. 95 00:06:52,590 --> 00:06:55,860 It will only create it if it does not already exist. 96 00:06:55,860 --> 00:07:01,470 Now one thing I want to say one last thing about scheme us we can freely add in and remove properties 97 00:07:01,500 --> 00:07:02,730 as we see fit. 98 00:07:02,940 --> 00:07:07,650 So at some point we might decide that we want to get our users name like their first name. 99 00:07:07,800 --> 00:07:14,670 We can easily add in an additional property without any mass whatsoever so freely add or subtract properties 100 00:07:14,670 --> 00:07:17,070 here as you please. 101 00:07:17,070 --> 00:07:17,640 OK. 102 00:07:17,910 --> 00:07:24,640 One last thing remember that whenever we create a javascript file inside of our project just creating 103 00:07:24,640 --> 00:07:27,820 it does not mean that the file actually gets executed. 104 00:07:27,900 --> 00:07:33,900 So we need to make sure that we actually require this file to somewhere inside of our project structure. 105 00:07:33,900 --> 00:07:45,060 So right now I'm going to change on over to my index file and I will require in the models slash user 106 00:07:45,060 --> 00:07:46,760 file that we just created. 107 00:07:47,130 --> 00:07:52,850 So that will ensure that whenever our application first boot up all of this configuration will load 108 00:07:53,220 --> 00:07:59,160 and longus will be informed that it needs to be responsible for creating a collection of users. 109 00:07:59,160 --> 00:07:59,450 OK. 110 00:07:59,460 --> 00:08:00,360 So that's pretty much it. 111 00:08:00,360 --> 00:08:03,410 We have now created our first model class. 112 00:08:03,480 --> 00:08:08,940 This right here loading this model into mongoose that creates our model class. 113 00:08:09,300 --> 00:08:17,850 So let's now continue in the next section and we will use that model class to create a new record inside 114 00:08:17,850 --> 00:08:20,310 of our list of all of our different users. 115 00:08:20,310 --> 00:08:25,500 Any time a user signs into our application for the very first time so I'll see you in just a second.