1 00:00:01,090 --> 00:00:03,370 Let's now start creating documents, 2 00:00:03,370 --> 00:00:06,903 testing the model, and really start using Mongoose. 3 00:00:08,510 --> 00:00:10,570 And I'm really excited about this video, 4 00:00:10,570 --> 00:00:12,823 so really let's get started here. 5 00:00:14,130 --> 00:00:17,143 So what we're gonna do is to create a new variable, 6 00:00:19,000 --> 00:00:21,373 and I'm gonna call it testTour, 7 00:00:22,510 --> 00:00:24,400 and this will be a new document 8 00:00:24,400 --> 00:00:26,500 created out of the tour model 9 00:00:26,500 --> 00:00:28,460 that we created in the last lecture. 10 00:00:28,460 --> 00:00:29,520 So what we're gonna do 11 00:00:29,520 --> 00:00:33,110 is to say new Tour, 12 00:00:33,110 --> 00:00:36,443 and then in there pass an object with the data. 13 00:00:38,020 --> 00:00:39,913 So name, let's say, 14 00:00:41,920 --> 00:00:44,100 again, 'The Forest Hiker,' 15 00:00:44,100 --> 00:00:46,463 just as we've been using before, 16 00:00:47,630 --> 00:00:49,513 then the rating, let's say 4.7, 17 00:00:53,157 --> 00:00:54,740 and then the price, 18 00:00:56,010 --> 00:00:58,643 let's say 497, okay? 19 00:00:59,690 --> 00:01:02,570 So, this is, again, a new document 20 00:01:02,570 --> 00:01:04,890 that we created out of a tour model, 21 00:01:04,890 --> 00:01:06,610 or function constructors, 22 00:01:06,610 --> 00:01:08,830 and so as I said before, this is kinda 23 00:01:08,830 --> 00:01:11,390 using JavaScript function constructors, 24 00:01:11,390 --> 00:01:14,660 or JavaScript classes if you're using ES6, 25 00:01:14,660 --> 00:01:16,440 basically to create new objects 26 00:01:16,440 --> 00:01:18,370 out of a class, right, 27 00:01:18,370 --> 00:01:21,020 and so that's exactly the same syntax, actually, 28 00:01:21,020 --> 00:01:22,660 so we use the name keyword, 29 00:01:22,660 --> 00:01:24,780 and then the class name in ES6, 30 00:01:24,780 --> 00:01:27,730 just like this, all right? 31 00:01:27,730 --> 00:01:29,930 Now here, it's not exactly the same, 32 00:01:29,930 --> 00:01:31,890 but this analogy is helpful 33 00:01:31,890 --> 00:01:33,890 to understand how this works. 34 00:01:33,890 --> 00:01:36,710 So this test tour document that we just created 35 00:01:36,710 --> 00:01:39,380 is an instance of the tour model, 36 00:01:39,380 --> 00:01:41,670 and so now it has a couple of methods on it 37 00:01:41,670 --> 00:01:43,010 that we can use in order to 38 00:01:43,010 --> 00:01:44,873 interact with the database. 39 00:01:45,890 --> 00:01:47,750 So let me show that to you. 40 00:01:47,750 --> 00:01:52,030 So we can now say testTour.save, 41 00:01:52,030 --> 00:01:53,550 and this will then save it to 42 00:01:53,550 --> 00:01:56,810 the tours collection in the database, okay, 43 00:01:56,810 --> 00:01:59,060 and it's really as simple as that. 44 00:01:59,060 --> 00:02:01,700 So again, we have our document instance 45 00:02:01,700 --> 00:02:03,730 which is testTour, and on there 46 00:02:03,730 --> 00:02:05,870 we can then call the save method 47 00:02:05,870 --> 00:02:08,653 in order to save the document to the database. 48 00:02:09,490 --> 00:02:11,310 Now this save here will then return 49 00:02:11,310 --> 00:02:13,453 a promise that we can then consume. 50 00:02:15,010 --> 00:02:17,920 So for now, let's use then for that, 51 00:02:17,920 --> 00:02:19,797 and in the future, we will actually use 52 00:02:19,797 --> 00:02:23,340 async await in order to consume these promises, 53 00:02:23,340 --> 00:02:24,830 but for now, let's keep it simple 54 00:02:24,830 --> 00:02:26,880 and not create any more functions, 55 00:02:26,880 --> 00:02:28,750 and so instead, we're just using 56 00:02:28,750 --> 00:02:30,550 the then method here. 57 00:02:30,550 --> 00:02:33,010 So in here, we get access to the document 58 00:02:33,010 --> 00:02:35,123 that was just saved to the database. 59 00:02:36,920 --> 00:02:39,850 So, basically, the result value of the promise 60 00:02:39,850 --> 00:02:42,180 that the save method returns is 61 00:02:42,180 --> 00:02:45,690 the final document as it is in the database. 62 00:02:45,690 --> 00:02:47,770 And so let's now actually go ahead 63 00:02:47,770 --> 00:02:51,293 and log the document to the console, all right? 64 00:02:52,290 --> 00:02:54,620 Now, on the other hand, saving this document 65 00:02:54,620 --> 00:02:57,030 to the database might also go wrong, 66 00:02:57,030 --> 00:02:59,749 and so let's catch that error, okay? 67 00:02:59,749 --> 00:03:02,100 So just like before, we used the catch 68 00:03:02,100 --> 00:03:05,400 method in order to catch any potential errors, 69 00:03:05,400 --> 00:03:07,460 and so if there is an error when 70 00:03:07,460 --> 00:03:09,430 saving the document to the database, 71 00:03:09,430 --> 00:03:11,130 we will be able to catch that error 72 00:03:11,130 --> 00:03:12,223 here in this method. 73 00:03:14,820 --> 00:03:17,510 Okay, so basically, here in this function 74 00:03:17,510 --> 00:03:19,103 that I'm passing into catch. 75 00:03:21,570 --> 00:03:24,710 Okay, for now, all I wanna do, really, 76 00:03:24,710 --> 00:03:26,623 is to also log it to the console. 77 00:03:27,660 --> 00:03:30,470 Let's say, 'Error,' add some emojis here 78 00:03:30,470 --> 00:03:32,800 to make it more visible in the console, 79 00:03:32,800 --> 00:03:35,154 and then we can pass in a second argument 80 00:03:35,154 --> 00:03:37,990 into the console log, which will 81 00:03:37,990 --> 00:03:40,490 then be the error, okay? 82 00:03:40,490 --> 00:03:41,323 Are you ready? 83 00:03:41,323 --> 00:03:43,170 Then, let's give it a save now, 84 00:03:43,170 --> 00:03:46,260 and that will then execute the code that is in here, 85 00:03:46,260 --> 00:03:49,520 so basically create this new tour, and try to save it 86 00:03:49,520 --> 00:03:53,090 to the database right away, okay? 87 00:03:53,090 --> 00:03:54,410 So, let's give it a save now, 88 00:03:54,410 --> 00:03:56,433 and see what happens in the console, 89 00:03:58,170 --> 00:03:59,733 and indeed, here we go. 90 00:04:01,260 --> 00:04:03,700 So you see, that this is the document 91 00:04:03,700 --> 00:04:07,580 that was just saved to our database, all right? 92 00:04:07,580 --> 00:04:10,350 So here we actually see the ID as well, 93 00:04:10,350 --> 00:04:13,451 and that is simply auto-generated by MongoDB, 94 00:04:13,451 --> 00:04:16,770 and it's simply the unique identifier for the document. 95 00:04:16,770 --> 00:04:19,940 So just like it happened before in the MongoDB section, 96 00:04:19,940 --> 00:04:22,800 we automatically get this unique identifier 97 00:04:22,800 --> 00:04:24,290 for the new object. 98 00:04:24,290 --> 00:04:26,960 Then Mongoose also automatically adds 99 00:04:26,960 --> 00:04:29,800 this V, and set it to zero here, 100 00:04:29,800 --> 00:04:32,770 but that's not really important at this point, okay? 101 00:04:32,770 --> 00:04:35,090 What matters here is that our new document 102 00:04:35,090 --> 00:04:38,510 was actually successfully added to the database, 103 00:04:38,510 --> 00:04:40,920 and just to make sure, let's actually move over 104 00:04:40,920 --> 00:04:45,570 to Compass and take a look at our database there. 105 00:04:45,570 --> 00:04:48,150 So here, we have the tours collection, 106 00:04:48,150 --> 00:04:51,850 and then we have the document that we just created. 107 00:04:51,850 --> 00:04:54,940 So it has that exact same ID, and of course 108 00:04:54,940 --> 00:04:57,410 all the other data that we put in there, 109 00:04:57,410 --> 00:04:59,230 and so indeed, this is the object 110 00:04:59,230 --> 00:05:00,543 that we just created. 111 00:05:01,550 --> 00:05:03,960 And keep in mind that we had actually deleted 112 00:05:03,960 --> 00:05:05,830 the tours collection here in one 113 00:05:05,830 --> 00:05:07,800 of the previous videos, right, 114 00:05:07,800 --> 00:05:10,010 but Mongoose automatically created 115 00:05:10,010 --> 00:05:12,070 this new collection here as soon 116 00:05:12,070 --> 00:05:13,960 as we created the first document 117 00:05:13,960 --> 00:05:15,730 using the tour model, 118 00:05:15,730 --> 00:05:17,250 and so this name here basically 119 00:05:17,250 --> 00:05:19,820 comes from that tour model, it simply 120 00:05:19,820 --> 00:05:23,730 gives us this plural name, just as we had before. 121 00:05:23,730 --> 00:05:26,870 Perfect, we just made our express application 122 00:05:26,870 --> 00:05:29,660 really interact with a MongoDB database 123 00:05:29,660 --> 00:05:31,060 for the very first time, 124 00:05:31,060 --> 00:05:34,690 and that's actually really amazing, if you ask me. 125 00:05:34,690 --> 00:05:35,900 So this means that right now, 126 00:05:35,900 --> 00:05:39,343 we are able to create documents right from our code. 127 00:05:40,560 --> 00:05:43,700 So, that makes me really happy, 128 00:05:43,700 --> 00:05:45,670 and so let's actually try it again 129 00:05:45,670 --> 00:05:48,140 with another one, all right, 130 00:05:48,140 --> 00:05:49,930 but actually before we do that, 131 00:05:49,930 --> 00:05:52,630 I want to just reload this file here 132 00:05:52,630 --> 00:05:55,710 which will then try to save this document here 133 00:05:55,710 --> 00:05:58,400 to the database again, right, 134 00:05:58,400 --> 00:06:00,183 simply because all this code here is run 135 00:06:00,183 --> 00:06:03,350 each time that we save the file. 136 00:06:03,350 --> 00:06:05,945 Now, let's try to guess what is gonna happen 137 00:06:05,945 --> 00:06:07,730 as soon as I save this file. 138 00:06:07,730 --> 00:06:08,563 Can you guess? 139 00:06:09,930 --> 00:06:11,610 Let's wait for it, 140 00:06:11,610 --> 00:06:14,160 and indeed we get something different now, 141 00:06:14,160 --> 00:06:16,000 we get an error here, 142 00:06:16,000 --> 00:06:18,260 and the error that we see is this 143 00:06:18,260 --> 00:06:20,940 duplicate key error collection, 144 00:06:20,940 --> 00:06:25,940 which down here says this duplicate key, okay? 145 00:06:26,210 --> 00:06:28,520 So we already have a tour with the name 146 00:06:28,520 --> 00:06:30,680 of 'Forest Hiker,' and so now we were 147 00:06:30,680 --> 00:06:32,843 trying to create another one, 148 00:06:32,843 --> 00:06:35,170 and since in our schema we have this 149 00:06:35,170 --> 00:06:37,817 unique property here, that is not allowed, 150 00:06:37,817 --> 00:06:39,370 remember that? 151 00:06:39,370 --> 00:06:40,852 So we set this unique here to true 152 00:06:40,852 --> 00:06:43,250 in order to make it impossible 153 00:06:43,250 --> 00:06:45,810 to have two tours with the same name, 154 00:06:45,810 --> 00:06:48,743 and so we just proved that this is actually working. 155 00:06:49,580 --> 00:06:51,990 All right, so let's simple replace 156 00:06:51,990 --> 00:06:53,370 the code that we have here 157 00:06:53,370 --> 00:06:56,173 in order to create a new tour with another name. 158 00:06:57,060 --> 00:06:59,917 Let's call it 'The Park Camper,' 159 00:07:01,640 --> 00:07:04,560 and let's go ahead and remove the price 160 00:07:04,560 --> 00:07:05,943 and also the rating. 161 00:07:06,890 --> 00:07:09,403 So, can you guess what's gonna happen now? 162 00:07:10,280 --> 00:07:12,450 Let's actually clear, or actually it 163 00:07:12,450 --> 00:07:14,370 already loaded it because as soon 164 00:07:14,370 --> 00:07:17,190 as I clicked here, it then saved the file, 165 00:07:17,190 --> 00:07:20,560 and so, yeah we just got another error, 166 00:07:20,560 --> 00:07:22,530 so you can see that up here, 167 00:07:22,530 --> 00:07:24,620 meaning that we didn't successfully save 168 00:07:24,620 --> 00:07:27,170 this document to the database, 169 00:07:27,170 --> 00:07:30,200 and what we got here now is a validation error 170 00:07:30,200 --> 00:07:32,090 coming from the price field 171 00:07:32,090 --> 00:07:34,710 saying 'A Tour must have a price,' 172 00:07:34,710 --> 00:07:36,670 and so that is exactly the message 173 00:07:36,670 --> 00:07:39,970 that we specified up here, right? 174 00:07:39,970 --> 00:07:42,160 Simply because in this line we said 175 00:07:42,160 --> 00:07:45,320 that the price is a required field, all right, 176 00:07:45,320 --> 00:07:47,050 and so this required, as I mentioned, 177 00:07:47,050 --> 00:07:49,750 is a validator, and so down here, 178 00:07:49,750 --> 00:07:52,260 we then get a validation error. 179 00:07:52,260 --> 00:07:53,520 And so let's now fix that 180 00:07:55,860 --> 00:08:00,050 and specify the price, let's say 997, 181 00:08:00,050 --> 00:08:01,460 and give it another save. 182 00:08:01,460 --> 00:08:03,310 Now this time, it should work 183 00:08:03,310 --> 00:08:06,010 because the rating is not required, 184 00:08:06,010 --> 00:08:07,780 and so when we come down here, 185 00:08:07,780 --> 00:08:09,930 our document was actually successfully 186 00:08:09,930 --> 00:08:13,060 saved to the database, you see? 187 00:08:13,060 --> 00:08:15,450 Now, we didn't specify the rating here 188 00:08:15,450 --> 00:08:19,240 in this test tour, but you see it's still 4.5, 189 00:08:19,240 --> 00:08:20,800 and that is because we set 190 00:08:20,800 --> 00:08:23,770 the default to 4.5 up here. 191 00:08:23,770 --> 00:08:24,750 So I just wanted to show you 192 00:08:24,750 --> 00:08:26,763 that here in this quick example. 193 00:08:27,770 --> 00:08:30,810 Okay, so we were successful again, 194 00:08:30,810 --> 00:08:33,210 so let's come back here, reload, 195 00:08:33,210 --> 00:08:35,690 you're clicking this button, and indeed 196 00:08:35,690 --> 00:08:37,840 we now get our two tours. 197 00:08:37,840 --> 00:08:40,930 Great, and I remember how amazing it felt 198 00:08:40,930 --> 00:08:43,370 for me when this worked the first time 199 00:08:43,370 --> 00:08:46,250 when I was starting to learn Express and Mongo, 200 00:08:46,250 --> 00:08:48,120 and how to build these applications, 201 00:08:48,120 --> 00:08:50,240 and so I really hope that you're 202 00:08:50,240 --> 00:08:52,130 feeling the same way right now, 203 00:08:52,130 --> 00:08:54,100 as you just created your very first 204 00:08:54,100 --> 00:08:56,033 documents using Express 205 00:08:56,033 --> 00:08:57,823 and, of course, MongoDB.