1 00:00:00,770 --> 00:00:05,950 In the last section we had a very long discussion about how we would be using lot to uniquely identify 2 00:00:05,950 --> 00:00:08,170 users inside of our application. 3 00:00:08,440 --> 00:00:13,930 In this video we're now going to continue by talking about the basics of Mongo D-B and then in the next 4 00:00:13,930 --> 00:00:17,430 section we will start to wired up inside of our application. 5 00:00:17,590 --> 00:00:24,040 Once we have Lango D-B set up inside of our application we can then make sure that we create a new user 6 00:00:24,130 --> 00:00:28,730 record every time someone signs into our application using cloth. 7 00:00:28,780 --> 00:00:34,030 After that we can then get back into our waterflow making sure that we deal with this cookie thing and 8 00:00:34,060 --> 00:00:38,990 all that kind of associated logic that we will make sure to review over time. 9 00:00:39,040 --> 00:00:40,080 So let's get to it. 10 00:00:40,090 --> 00:00:43,050 Let's start talking about the basics of Monga. 11 00:00:43,810 --> 00:00:48,880 We're going to start by reviewing a diagram that we are looked at much earlier inside this course. 12 00:00:48,880 --> 00:00:51,190 But with one small change to it. 13 00:00:51,310 --> 00:00:55,690 So we had previously looked at this diagram where we had said that we have our re-act up running inside 14 00:00:55,690 --> 00:00:59,620 the browser and no we haven't done this yet but we will very shortly. 15 00:00:59,890 --> 00:01:06,400 We had said at the re-act up would communicate with our express API by exchanging age TTP requests that 16 00:01:06,400 --> 00:01:13,360 contain little snippets of Jason data we haven't previously said that express would interact with our 17 00:01:13,360 --> 00:01:18,880 Mongo divi database to store records and pull out existing records. 18 00:01:18,880 --> 00:01:25,000 Now I've made one change to this diagram where I've added in the additional box here for a library called 19 00:01:25,090 --> 00:01:27,240 mongoose J.S.. 20 00:01:27,580 --> 00:01:32,410 Mongoose is a optional library that we are going to be using inside this course. 21 00:01:32,410 --> 00:01:39,000 The sole purpose of mongoose is to make our lives easier when working with Mongo D-B. 22 00:01:39,070 --> 00:01:41,220 So again it is completely optional. 23 00:01:41,290 --> 00:01:46,240 In general it is required in this course but in general it is optional but I highly recommend you use 24 00:01:46,240 --> 00:01:52,690 it because it wraps up a lot of very common operations that you might have to do by hand when working 25 00:01:52,690 --> 00:01:54,320 with Mongo. 26 00:01:54,340 --> 00:02:00,310 So as we start to talk about Mongo inside of this course we're really going to be covering two separate 27 00:02:00,310 --> 00:02:01,790 and distinct topics. 28 00:02:01,900 --> 00:02:07,690 On the one hand we will be talking about Mongo D.B and on the other hand we will be talking about mongoose 29 00:02:07,750 --> 00:02:12,140 and how we use it to interact with the data that sits inside of Lango. 30 00:02:12,770 --> 00:02:13,300 OK. 31 00:02:13,540 --> 00:02:15,300 So let's start that process now. 32 00:02:15,460 --> 00:02:21,800 The first thing we're going to discuss is how Mongo internally stores information. 33 00:02:21,840 --> 00:02:23,120 So here's Monga. 34 00:02:23,530 --> 00:02:30,390 Mungo internally stores records into different collections every different collection that sits inside 35 00:02:30,420 --> 00:02:36,420 of our database can have many different records and we can have many different collections. 36 00:02:36,420 --> 00:02:43,020 So inside of one Mongo divi instance we might have a collection of users a collection of posts and a 37 00:02:43,020 --> 00:02:49,110 collection of payments inside of a single collection like let's say the users collection. 38 00:02:49,110 --> 00:02:54,680 We have many different individual records in the context of a user's collection. 39 00:02:54,720 --> 00:03:00,240 We might imagine that every single one of these records represents someone who has signed in or signed 40 00:03:00,240 --> 00:03:01,710 up to our application. 41 00:03:01,800 --> 00:03:07,570 So we've got one record that represents a user with the name of Anna and another record that represents. 42 00:03:07,590 --> 00:03:15,960 Alex bill and so on every single record is essentially a little piece of Jaison or essentially a plain 43 00:03:15,960 --> 00:03:17,720 javascript object. 44 00:03:17,730 --> 00:03:22,200 In other words every single record is a collection of key value pairs. 45 00:03:22,410 --> 00:03:26,180 So I can have a name property with a value of Anna. 46 00:03:26,600 --> 00:03:31,250 And let's zoom in here really quick a height with a value of 150. 47 00:03:31,380 --> 00:03:37,940 And then in the next object over or the next record over I might have an ID a name and an H. 48 00:03:38,220 --> 00:03:44,400 One of the important defining characteristics of Mongo D-B is the fact that it is what we refer to as 49 00:03:44,400 --> 00:03:46,380 being schema Elance. 50 00:03:46,440 --> 00:03:54,720 In other words inside of one single collection every record can have its own very distinct set of properties. 51 00:03:54,720 --> 00:04:00,750 So you'll notice that for example this first record right here has a height property but the second 52 00:04:00,750 --> 00:04:05,660 one doesn't nor the third but the fourth one does likewise. 53 00:04:05,790 --> 00:04:11,650 The second record right here has an age property but no other record in here does. 54 00:04:11,760 --> 00:04:18,660 This is in direct contrast to traditional databases like siecle type databases or relational databases 55 00:04:19,020 --> 00:04:23,940 where traditionally every single record has to have the exact same properties. 56 00:04:24,120 --> 00:04:31,170 So in other words everything would have to have an ID a name a height an age defined so that is one 57 00:04:31,380 --> 00:04:37,740 distinct difference that separates Mungo DVH from other databases like say My sequel or postscripts. 58 00:04:37,870 --> 00:04:42,660 And let me be clear One difference among many many other differences as well. 59 00:04:43,390 --> 00:04:43,710 OK. 60 00:04:43,740 --> 00:04:47,220 So that is a little bit into the background of Mongo D-B. 61 00:04:47,220 --> 00:04:52,160 Remember we have many different collections in every collection has many different records. 62 00:04:52,260 --> 00:04:57,870 You can think of every record as being a single javascript object that has a couple of different key 63 00:04:57,870 --> 00:04:59,710 value pairs. 64 00:04:59,730 --> 00:05:06,620 Now the next thing I want to talk to you about very briefly is what Mongoose is doing for us. 65 00:05:06,780 --> 00:05:15,200 So we're going to first talk a little bit about exactly how mongoose relates to Mongo D-B So on the 66 00:05:15,200 --> 00:05:18,350 bottom half of this diagram will focus on this bottom half. 67 00:05:18,350 --> 00:05:26,120 First we had just one second ago said that in Lango D-B we have a bunch of different collections and 68 00:05:26,180 --> 00:05:32,900 every collection has a bunch of different records to represent this type of structure in the javascript 69 00:05:32,900 --> 00:05:37,530 world or in the expressed code or the no J.S. code that you and I are going to write. 70 00:05:37,840 --> 00:05:43,240 We are going to have two different very important concepts that are implemented by Munger's. 71 00:05:43,520 --> 00:05:46,680 So just to be clear these are like mongoose things up here. 72 00:05:46,730 --> 00:05:53,400 Let me label this as being like this is from mongoose. 73 00:05:53,480 --> 00:06:00,470 So in the mongoose world or by using the mongoose library we make use of something called a model class 74 00:06:01,190 --> 00:06:08,570 a model a class created with Mongoose represents an entire Mongo DB collection. 75 00:06:08,630 --> 00:06:16,370 So the model class is used to access a single collection sitting inside of Mongo D-B the model class 76 00:06:16,430 --> 00:06:22,060 has a bunch of functions attached to it that are designed to work with an entire collection. 77 00:06:22,210 --> 00:06:28,760 And so doing things like say creating a new record or searching all the records inside of our collection 78 00:06:29,390 --> 00:06:32,700 is done by using a model class. 79 00:06:33,350 --> 00:06:38,420 Mongoose also gives us access to something called Model instances. 80 00:06:38,420 --> 00:06:42,560 And so to be clear again this is all from mongoose model. 81 00:06:42,650 --> 00:06:50,120 Instances are all javascript objects that represent a single record sitting inside of a collection. 82 00:06:50,120 --> 00:06:57,230 So in practice we end up with one modeled class representing one collection inside of Mongo DVH and 83 00:06:57,230 --> 00:07:04,430 then we end up with many instances each of which representing one of the records inside of our Mago 84 00:07:04,430 --> 00:07:05,750 DVD collection. 85 00:07:06,170 --> 00:07:09,640 So again one more time because this is a very important distinction. 86 00:07:09,640 --> 00:07:17,420 One class is related to one collection we have a bunch of instances each of which represent one individual 87 00:07:17,510 --> 00:07:18,620 record. 88 00:07:19,310 --> 00:07:19,890 OK. 89 00:07:20,210 --> 00:07:22,350 So I think there might be one more slide here. 90 00:07:22,350 --> 00:07:24,020 No that's about it. 91 00:07:24,020 --> 00:07:28,430 So I think that's enough for a very quick introduction to Mongo and mongoose. 92 00:07:28,430 --> 00:07:34,100 Now I do want to tell you right now that inside like the first half of this course we're not really 93 00:07:34,100 --> 00:07:37,820 going to using mongoose in Lango a tremendous amount. 94 00:07:37,850 --> 00:07:41,360 So right now we're going to go through a little bit of the basic set up and we're going to use it in 95 00:07:41,360 --> 00:07:47,720 a very limited basic way but later on in the course like in the last quarter we're going to come back 96 00:07:47,720 --> 00:07:52,430 to Mongo and we're going to talk about many more topics around it so as to make sure we're on the same 97 00:07:52,430 --> 00:07:54,820 page about what we're talking about when. 98 00:07:55,220 --> 00:07:55,610 OK. 99 00:07:55,670 --> 00:07:57,400 So I think this is enough the introduction. 100 00:07:57,620 --> 00:07:58,400 Let's take a break. 101 00:07:58,400 --> 00:08:03,270 We're going to come back in the next section and we're going to start to set up our Mongo DB database.