0 1 00:00:00,370 --> 00:00:00,740 All right. 1 2 00:00:00,750 --> 00:00:07,080 So in the previous module we looked at how we would use MongoDB in the command line through the use 2 3 00:00:07,140 --> 00:00:08,380 of the Mongo shell. 3 4 00:00:08,430 --> 00:00:16,380 And that's pretty much MongoDB in its purest form in an isolated setting. But that's no use to us because 4 5 00:00:16,380 --> 00:00:19,230 we want to build applications that have databases 5 6 00:00:19,230 --> 00:00:19,800 right? 6 7 00:00:19,860 --> 00:00:25,860 So we need to learn how to integrate all MongoDB database with on Node.js application. 7 8 00:00:25,860 --> 00:00:28,680 And that is what this module is all about. 8 9 00:00:29,850 --> 00:00:35,270 Now when you're creating a Node app that needs to connect to a MongoDB database 9 10 00:00:35,430 --> 00:00:39,080 there's essentially two options for you to choose from. 10 11 00:00:39,180 --> 00:00:44,450 One is to use the MongoDB native driver that we're going to talk about very quickly. 11 12 00:00:44,700 --> 00:00:50,850 Another is to use a ODM or an Object Document Mapper that's called mongoose. 12 13 00:00:50,850 --> 00:00:57,570 Now the most popular way of working with MongoDB and Node.js is through using this package called 13 14 00:00:57,660 --> 00:00:58,920 mongoose. 14 15 00:00:58,920 --> 00:01:05,700 And the reason is because it vastly simplifies and cuts down on the code that's required in order to 15 16 00:01:05,700 --> 00:01:08,220 work with your MongoDB database. 16 17 00:01:08,340 --> 00:01:14,340 But I want to show you first how the native MongoDB driver would work and then we'll go through how 17 18 00:01:14,340 --> 00:01:21,300 to use mongoose just so that you can see for yourself just how much easier it is to incorporate mongoose 18 19 00:01:21,300 --> 00:01:24,550 and it makes your life as a developer so much better. 19 20 00:01:24,570 --> 00:01:30,970 But first let's get started and take a look at how we would use the MongoDB native driver. 20 21 00:01:30,990 --> 00:01:37,310 Firstly I want to head over to the MongoDB documentation at docs.mongodb.com. 21 22 00:01:37,530 --> 00:01:45,160 And here we're going to head over to the tab that's called drivers. And on the left hand pane here you can 22 23 00:01:45,160 --> 00:01:49,560 see that there's a whole bunch of drivers for different languages. 23 24 00:01:49,780 --> 00:01:56,950 Now the driver is what's going to enable our MongoDB to interact with our application and depending on 24 25 00:01:57,010 --> 00:02:02,770 which language your application was developed with then you'll need to use a different driver. 25 26 00:02:02,770 --> 00:02:10,000 Now in our case we're going to choose Node.js and we're going to view the latest documentation. And from 26 27 00:02:10,000 --> 00:02:15,940 here we're going to head over to quick starts and it details pretty much step by step how you would 27 28 00:02:15,940 --> 00:02:20,860 get started using the native MongoDB Node.js driver. 28 29 00:02:20,860 --> 00:02:22,540 Well that's quite a long sentence. 29 30 00:02:22,540 --> 00:02:29,080 So essentially what we're trying to do is we're trying to glue together our MongoDB database with 30 31 00:02:29,210 --> 00:02:31,090 our Node.js application. 31 32 00:02:31,090 --> 00:02:37,600 And this is going to be the syntax and the code and we're going to install the driver to enable us to 32 33 00:02:37,600 --> 00:02:38,210 do that. 33 34 00:02:39,140 --> 00:02:43,780 First things first, we're going to need a new project. For this project 34 35 00:02:43,790 --> 00:02:49,220 I've decided that I'm going to make a database of fruits and I'm going to rate all the fruits that I've 35 36 00:02:49,220 --> 00:02:50,000 tried. 36 37 00:02:50,000 --> 00:02:56,150 And there's some really wacky ones out there that I've come across through my travels and I think it's 37 38 00:02:56,150 --> 00:03:03,200 a pretty nice way of showing you how to work with MongoDB and Node. CD over to wherever you keep 38 39 00:03:03,230 --> 00:03:04,770 all of your development files 39 40 00:03:04,790 --> 00:03:11,360 and then we're going to create a new directory and that's going to be called FruitsProject. And then 40 41 00:03:11,420 --> 00:03:13,780 we're going to head over to FruitsProject 41 42 00:03:14,030 --> 00:03:17,060 and here we're going to create a new app. 42 43 00:03:17,120 --> 00:03:23,850 So we're going to create a new file called app.js and as per usual we're going to initialize our npm. 43 44 00:03:24,080 --> 00:03:30,980 But this time instead if you don't want to hit enter to insert the default values when you initialize 44 45 00:03:30,980 --> 00:03:36,380 npm, you can just add a -y which basically says yes to everything. 45 46 00:03:36,650 --> 00:03:43,260 And when you hit enter you can see that we've initialized our npm and accepted all of the defaults. 46 47 00:03:43,280 --> 00:03:48,330 So now all we have to do is to install the MongoDB driver. 47 48 00:03:49,150 --> 00:03:53,490 And to do that with npm we have to install something called mongodb. 48 49 00:03:53,800 --> 00:03:58,510 And remember we don't need the --save anymore as of the latest versions of npm. 49 50 00:03:58,720 --> 00:04:04,220 So we can simply say npm i short for install and then it's mongodb. 50 51 00:04:04,510 --> 00:04:05,470 Hit enter 51 52 00:04:05,480 --> 00:04:09,150 and that should install the driver in a couple of seconds. 52 53 00:04:09,730 --> 00:04:17,440 So now all we have to do is open up our app.js inside Atom and get started developing on your Node 53 54 00:04:17,460 --> 00:04:22,360 JS application that integrates with a MongoDB database. 54 55 00:04:22,390 --> 00:04:30,130 I'm just going to show you how we can get started using this by just using some the code from the getting 55 56 00:04:30,130 --> 00:04:31,330 started guide. 56 57 00:04:31,360 --> 00:04:41,150 So I'm simply going to copy this part and add it to our aappp.js and again we've got a whole bunch 57 58 00:04:41,150 --> 00:04:45,840 of warnings so let's just silence that from //jshint. And 58 59 00:04:49,910 --> 00:04:52,770 now I want to walk you through this code. First 59 60 00:04:52,780 --> 00:04:56,390 we are requiring a package called mongodb 60 61 00:04:56,710 --> 00:05:01,760 and along with that package we also insert another one called assert. 61 62 00:05:01,780 --> 00:05:06,040 Now everything that you see that is related to assert be it on 62 63 00:05:06,070 --> 00:05:10,060 Node.js applications or iOS applications, 63 64 00:05:10,060 --> 00:05:12,470 it's always to do with testing. 64 65 00:05:12,820 --> 00:05:19,270 And so you'll see that when we add a lot of the MongoDB code into our application, there's a lot of 65 66 00:05:19,340 --> 00:05:26,700 assert that just validates our data entry and our connection to the MongoDB database. 66 67 00:05:26,710 --> 00:05:34,030 Now the next part is the connection URL. And you can see this looks a little bit similar to what 67 68 00:05:34,030 --> 00:05:38,710 we've been using to develop websites on our local system. 68 69 00:05:38,710 --> 00:05:44,830 The important thing to note here is that when you're working with MongoDB the port that they use is 69 70 00:05:45,010 --> 00:05:48,070 pretty much always 27017 70 71 00:05:48,070 --> 00:05:51,850 and this is just some sort of arbitrary number that they've decided upon. 71 72 00:05:51,880 --> 00:05:58,400 This is the base URL for connecting to our database. And then we specify a database name 72 73 00:05:58,420 --> 00:06:07,270 so let's change it to fruitsDB and then we create a new Mongo client which is going to connect to 73 74 00:06:07,300 --> 00:06:13,640 our MongoDB database and if a fruitsDB doesn't exist then it will create it. 74 75 00:06:13,810 --> 00:06:20,920 And if all of that happened without any errors then we are going to log that we have connected successfully 75 76 00:06:20,950 --> 00:06:23,940 to the server. Once we're done 76 77 00:06:23,970 --> 00:06:26,680 we're going to close the connection to our database 77 78 00:06:26,910 --> 00:06:34,230 and this is all that this code does so far, the majority of it down here is just to create a new connection 78 79 00:06:34,530 --> 00:06:36,190 to a new database. 79 80 00:06:36,270 --> 00:06:43,440 And this is equivalent to when we saw in the Mongos shell previously where we just said use fruits 80 81 00:06:43,440 --> 00:06:51,570 DB that use fruitsDB if we wanted to use it in our Node.js application using the MongoDB native driver 81 82 00:06:51,870 --> 00:06:54,700 is basically all of this code. 82 83 00:06:54,750 --> 00:06:56,320 So it's a bit crazy. 83 84 00:06:56,400 --> 00:07:03,120 And the reason why I'm walking you through this and explaining the code is that in reality most developers 84 85 00:07:03,120 --> 00:07:09,040 who are working with Node and MongoDB will rarely use the native MongoDB driver. 85 86 00:07:09,210 --> 00:07:11,090 Now it's not because it's no good, 86 87 00:07:11,160 --> 00:07:17,550 it works and allows a lot of personalization and you can drill down to the specifics and you can set 87 88 00:07:17,550 --> 00:07:22,460 up and use your MongoDB database with a high level of control. 88 89 00:07:22,980 --> 00:07:30,300 All right so now that we have created this, let's go ahead and run our app.js using again node app 89 90 00:07:30,300 --> 00:07:38,710 .js and you can see we get a error. And it says MongoNetworkError failed to connect to server. 90 91 00:07:39,000 --> 00:07:41,950 And whenever you get this, you always have to remind yourself 91 92 00:07:42,000 --> 00:07:46,250 do you still have the Mongo server up and running? 92 93 00:07:46,350 --> 00:07:53,610 So create a new tab and remember whenever we're using MongoDB we always have to first run our server 93 94 00:07:53,910 --> 00:08:01,380 using the command mongod. And only once it's done running and it's waiting for connections on again 94 95 00:08:01,380 --> 00:08:03,840 that port 27017 95 96 00:08:03,840 --> 00:08:05,880 it's going to come up in a lot of places. 96 97 00:08:05,880 --> 00:08:12,450 Well now we're ready to actually connect to our database and we can run node ap.js again 97 98 00:08:12,600 --> 00:08:16,920 and now we are successfully connected to the server. 98 99 00:08:17,430 --> 00:08:23,760 If at some point you get a deprecation warning as you see here then just read what the message says 99 100 00:08:23,790 --> 00:08:26,660 because MongoDB, because it's relatively new, 100 101 00:08:26,820 --> 00:08:34,750 they will add new syntax and new code and new APIs, new ways of doing things, on a regular basis. 101 102 00:08:34,770 --> 00:08:40,770 So in this case it says deprecation warning current URL string parser is deprecated and will 102 103 00:08:40,770 --> 00:08:45,780 be removed in a future version. To use the new parser, pass option 103 104 00:08:45,780 --> 00:08:48,900 this to the MongoClient.connect. 104 105 00:08:49,050 --> 00:08:53,300 If you didn't know what to do with this deprecation warning, then what do you do? 105 106 00:08:53,310 --> 00:09:01,590 Well you just copy it in its entirety and then you paste it into Google and see what does Google say. 106 107 00:09:01,680 --> 00:09:09,150 So first link is a link to Stack Overflow and it already tells you this is how you would work around 107 108 00:09:09,150 --> 00:09:09,550 it. 108 109 00:09:09,690 --> 00:09:15,300 So you can either change to a local version of Mongo which is not what we want to do, we want to work with 109 110 00:09:15,300 --> 00:09:16,410 the latest version. 110 111 00:09:16,590 --> 00:09:22,260 So if you do indeed want to use latest version, then you will have to make your connection using this 111 112 00:09:22,460 --> 00:09:23,030 use 112 113 00:09:23,070 --> 00:09:25,930 NewUrlParser key value pair. 113 114 00:09:26,250 --> 00:09:29,670 And here they've got some example code of how you would add it. 114 115 00:09:29,670 --> 00:09:33,140 So they've added it to MongoClient.connect. 115 116 00:09:33,150 --> 00:09:38,490 You can see that even though we're taking the code from MongoDB using the latest version of their 116 117 00:09:38,490 --> 00:09:44,130 documentation stuff gets out of date and it's really important that you don't get fazed by every little 117 118 00:09:44,130 --> 00:09:48,840 thing that you see in the console and simply just Google for an answer. 118 119 00:09:49,260 --> 00:09:52,640 Let's go back to Atom and let's go ahead and fix this. 119 120 00:09:52,650 --> 00:09:59,110 We're going to change our Mongo client to use the URL that we specified for our MongoDB server 120 121 00:09:59,220 --> 00:10:05,210 but then we're going to add a comma and we're going to paste in that option that they wanted us to add. 121 122 00:10:05,550 --> 00:10:06,630 So there we go. 122 123 00:10:06,630 --> 00:10:09,900 Now let's hit save and let's rerun our app. 123 124 00:10:09,900 --> 00:10:15,680 You can see now we get connected successfully to server and we don't get any deprecation warnings. 124 125 00:10:16,170 --> 00:10:21,840 Even though the last time we did get this warning our app is still running and connected successfully 125 126 00:10:21,840 --> 00:10:23,850 so it didn't actually affect our app. 126 127 00:10:24,030 --> 00:10:29,460 But it's important that whenever you see a deprecation warning to just copy and paste it into Google 127 128 00:10:29,520 --> 00:10:32,360 and see what the existing solutions are. 128 129 00:10:32,580 --> 00:10:38,310 It's very very rare that you will be coming across a problem for the first time and you're the only 129 130 00:10:38,310 --> 00:10:39,520 person with that problem. 130 131 00:10:39,540 --> 00:10:40,930 It almost never happens. 131 132 00:10:42,590 --> 00:10:48,800 OK so now that we have it connected successfully to our Mongo server the next part is adding some 132 133 00:10:48,800 --> 00:10:51,300 data to our database. 133 134 00:10:51,330 --> 00:10:52,450 So how do we do that? 134 135 00:10:52,470 --> 00:10:58,960 Well it's back to the documentation and the next part is of course insert a document. 135 136 00:10:59,010 --> 00:11:06,900 So let's copy all of this code from the documentation and let's paste it below the last line that we 136 137 00:11:06,900 --> 00:11:07,910 have. 137 138 00:11:08,010 --> 00:11:14,820 So this code what it does is it will create a new collection and the collection that we're going to 138 139 00:11:14,820 --> 00:11:17,820 create is going to be called fruits. 139 140 00:11:17,900 --> 00:11:21,840 We've got a collection called fruits inside our fruitsDB 140 141 00:11:22,010 --> 00:11:31,560 And this is equivalent to when we used our new database and then we said db.fruits.insert. 141 142 00:11:31,940 --> 00:11:39,340 And then we get to insert some data in here. The data is in the same format as we saw before. 142 143 00:11:39,350 --> 00:11:46,810 It's a bunch of key value pairs and here they're inserting three items into a field called a 143 144 00:11:46,850 --> 00:11:49,480 and then they've added the values 1,2 ,3. 144 145 00:11:49,550 --> 00:11:55,190 So as we saw before in the Mono shell whenever you see a pair of curly brackets that's going to be 145 146 00:11:55,220 --> 00:11:58,370 an individual document or an individual record. 146 147 00:11:58,590 --> 00:12:02,370 And in our case we're going to insert three fruits. 147 148 00:12:02,540 --> 00:12:09,560 So I'm going to just paste in some code that I wrote a little bit earlier on and we'll just format this 148 149 00:12:09,560 --> 00:12:11,720 or that you can see it more clearly. 149 150 00:12:14,320 --> 00:12:21,000 So here I'm inserting three documents and each document is a fruit. 150 151 00:12:21,070 --> 00:12:24,110 So the first one has a name of apple, a score of 8 151 152 00:12:24,160 --> 00:12:30,360 and a review of 'great fruit'. And the rest of them are for orange and banana. 152 153 00:12:30,820 --> 00:12:37,630 So we're now creating this array of fruits and we're using the method insertMany which comes from MongoDB 153 154 00:12:37,630 --> 00:12:44,620 to insert all of these three documents into a collection called fruits inside a database called 154 155 00:12:44,620 --> 00:12:45,630 fruitsDB. 155 156 00:12:48,740 --> 00:12:54,300 And down here you can see that the next few lines are dedicated to validation. 156 157 00:12:54,320 --> 00:12:57,350 So they've added a whole bunch of asserts. 157 158 00:12:57,380 --> 00:13:02,570 And if we go through them line by line, this one says validate to make sure that there are no errors 158 159 00:13:02,600 --> 00:13:05,280 when we inserted our document. 159 160 00:13:05,720 --> 00:13:11,080 And the next ones ensure that we have three results that are inserted into our collection. 160 161 00:13:11,330 --> 00:13:20,790 And if that is so then we're going to log inserted three documents into the collection. And the next 161 162 00:13:20,790 --> 00:13:26,930 part is to simply run that function inside our insert documents function and they show you that client. 162 163 00:13:27,030 --> 00:13:29,130 connect should now look like this 163 164 00:13:29,130 --> 00:13:37,290 with this additional part. Up here inside client.connect instead of calling client.close we're 164 165 00:13:37,290 --> 00:13:44,060 going to paste that new code which calls insertDocuments and only once it's done inserting the documents 165 166 00:13:44,070 --> 00:13:48,550 do we close the connection to our database. 166 167 00:13:48,560 --> 00:13:56,390 So now let's hit save and let's again run our code with node app.js and you can see we've successfully 167 168 00:13:56,390 --> 00:14:01,890 connected to the server and we've inserted three documents into the collection. 168 169 00:14:02,210 --> 00:14:08,210 And you can actually test out the validation quite easily. Say if we were only to add 2 records to 169 170 00:14:08,240 --> 00:14:13,890 our database, then you will get an assertion error saying that 3 doesn't equal 2. 170 171 00:14:13,910 --> 00:14:18,660 Well you only inserted 2 items but I was expecting 3. 171 172 00:14:18,770 --> 00:14:25,040 So you're ready beginning to see the validation that we can place inside our app.js using the Mongo 172 173 00:14:25,040 --> 00:14:29,300 DB driver in order to validate the data that we're inserting. 173 174 00:14:29,420 --> 00:14:37,870 And you can see just how very flexible all of these asserts are. If I head into my command line and I 174 175 00:14:37,870 --> 00:14:47,830 create a new terminal and then I tap into the Mongo shell and I say show dbs, you can see now in 175 176 00:14:47,830 --> 00:14:53,710 addition to our shopDB which we created inside the Mongo shell we've also got this new database 176 177 00:14:53,710 --> 00:15:00,340 called fruitsDB and that one was not created using the Mongo shell but instead from Node.js 177 178 00:15:00,670 --> 00:15:02,740 using the MongoDB driver. 178 179 00:15:02,950 --> 00:15:05,600 And that's where the fruitsDB got created. 179 180 00:15:08,180 --> 00:15:18,410 And now if I say use fruitsDB then we can say show collections inside this db 180 181 00:15:18,430 --> 00:15:23,680 so we have this collection called fruits and that of course comes from this part 181 182 00:15:23,680 --> 00:15:30,100 when we created our new collection using the db.collection method. And then we inserted a whole 182 183 00:15:30,100 --> 00:15:30,770 bunch of data 183 184 00:15:30,790 --> 00:15:36,620 if you're a member so we should be able to say db.fruits.find. 184 185 00:15:36,790 --> 00:15:43,070 And we're going to leave the parentheses empty to show us all the data that we currently have. The keen 185 186 00:15:43,160 --> 00:15:50,460 eyed amongst you will spot that we have 5 records instead of what you might expect to be 3 because 186 187 00:15:50,460 --> 00:15:56,430 the first time we added apple, orange, banana and the second time we deleted the banana record and we 187 188 00:15:56,430 --> 00:15:58,590 got an assertion failure. 188 189 00:15:58,650 --> 00:16:03,710 Now assertion is quite a complex topic but in this case this is a nonfatal assertion. 189 190 00:16:03,720 --> 00:16:10,770 So one actually prevent the insertion of these records but it simply alerts you to the fact that the 190 191 00:16:10,770 --> 00:16:13,910 assertion has failed. 191 192 00:16:14,010 --> 00:16:17,200 So the next step is how do we get our app. 192 193 00:16:17,280 --> 00:16:25,500 js to find all of these records? How do we read our data inside our Node.js app? 193 194 00:16:25,500 --> 00:16:27,970 Well it's only a short scroll away. 194 195 00:16:28,020 --> 00:16:34,150 So the next part is find all documents and let's just go ahead and add this into our code 195 196 00:16:35,410 --> 00:16:40,780 right at the bottom again and this section we have to specify the collection where we want to find something 196 197 00:16:40,780 --> 00:16:44,320 from which if you remember earlier on was called fruits, 197 198 00:16:44,340 --> 00:16:50,470 so let's change that to fruits. And then it's going to look through the collection and it's going to 198 199 00:16:50,470 --> 00:16:51,830 find all. 199 200 00:16:51,940 --> 00:16:54,490 And then it's going to save it into an array 200 201 00:16:54,670 --> 00:17:01,300 and for that array we need to have some fruits. And in the callback 201 202 00:17:01,330 --> 00:17:08,590 we're going to get back the object that we got from the find function and we're going to log those fruits 202 203 00:17:08,710 --> 00:17:10,140 into the console. 203 204 00:17:12,690 --> 00:17:21,020 Now if we hit save and call that find function inside again the client.connect, 204 205 00:17:21,270 --> 00:17:24,240 in this case we're not going to insert any more documents. 205 206 00:17:24,240 --> 00:17:26,780 We're simply just going to find our documents. 206 207 00:17:26,820 --> 00:17:31,500 So I'm just going to copy that part of the code and we're going to update this method 207 208 00:17:31,500 --> 00:17:40,040 client.connect to delete the insertion method call and just find those records inside our database. 208 209 00:17:40,350 --> 00:17:45,280 Let's head back to our terminal connection where we've got our prompt and let's call node app. 209 210 00:17:45,290 --> 00:17:46,390 js again. 210 211 00:17:46,620 --> 00:17:54,900 So now you can see that we've found the following records and this printed in here is an array of Javascript 211 212 00:17:54,960 --> 00:18:01,710 objects and you can see it looks very similar to what we've got in the Mongo shell but what's logged 212 213 00:18:01,710 --> 00:18:04,850 here are actually the documents from our MongoDB 213 214 00:18:05,070 --> 00:18:10,120 but here we've actually got our Javascript objects. 214 215 00:18:10,310 --> 00:18:17,810 So if we now have access to these living Javascript objects inside an array in our app.js then we 215 216 00:18:17,810 --> 00:18:23,790 can use our Javascript code and Node.js to do whatever it is that we wish with it. 216 217 00:18:24,200 --> 00:18:31,670 As you can see this code is very very wordy and there's a lot of it that's just boilerplate code that 217 218 00:18:31,670 --> 00:18:36,430 you have to add every single time you use the native MongoDB driver. 218 219 00:18:36,590 --> 00:18:42,300 And for some people especially if you want to develop applications quickly it can be a bit of a pain. 219 220 00:18:42,320 --> 00:18:49,130 So in the coming modules I want to show you what most Node developers will work with which is a module called 220 221 00:18:49,490 --> 00:18:56,090 mongoose and it will vastly simplify the code that we have here and we will walk through how to use 221 222 00:18:56,090 --> 00:19:02,180 mongoose to make your life developing Mongo and node apps so much easier. 222 223 00:19:02,270 --> 00:19:03,210 So I'll see you there.