1 00:00:01,130 --> 00:00:02,400 Welcome back. 2 00:00:02,400 --> 00:00:05,260 So after some heavier theory lectures, 3 00:00:05,260 --> 00:00:09,210 now, we're finally gonna start implementing our data model 4 00:00:09,210 --> 00:00:11,640 and starting with the locations. 5 00:00:11,640 --> 00:00:14,080 So in this video, you're gonna learn all about 6 00:00:14,080 --> 00:00:16,680 geospatial data in MongoDB. 7 00:00:16,680 --> 00:00:18,120 And this is really a topic 8 00:00:18,120 --> 00:00:20,513 that I personally find really exciting. 9 00:00:22,170 --> 00:00:24,630 Now, remember from the previous lecture 10 00:00:24,630 --> 00:00:27,470 that our location data will actually be embedded 11 00:00:27,470 --> 00:00:29,070 into the tours. 12 00:00:29,070 --> 00:00:31,680 And so therefore, we're basically gonna declare 13 00:00:31,680 --> 00:00:34,040 everything that is related to locations 14 00:00:34,040 --> 00:00:35,500 in our tour model. 15 00:00:35,500 --> 00:00:40,320 All right, so let's open that one up and right at the end, 16 00:00:40,320 --> 00:00:43,010 let's start by adding the startLocation. 17 00:00:43,010 --> 00:00:45,660 Okay, so we will have startLocation 18 00:00:45,660 --> 00:00:48,520 and then also, locations in general. 19 00:00:48,520 --> 00:00:52,680 Now, MongoDB supports geospatial data out of the box. 20 00:00:52,680 --> 00:00:56,650 And geospatial data is basically data that describes 21 00:00:56,650 --> 00:01:00,870 places on earth using longitude and latitude coordinates. 22 00:01:00,870 --> 00:01:03,530 Okay, so we can describe simple points 23 00:01:03,530 --> 00:01:06,350 or we can also describe more complex geometries 24 00:01:06,350 --> 00:01:11,350 like lines or even polygons or even multi-polygons. 25 00:01:11,350 --> 00:01:13,030 So really, everything is possible 26 00:01:13,030 --> 00:01:16,140 with geospatial data in MongoDB. 27 00:01:16,140 --> 00:01:20,060 Okay, so let's add our startLocation field here 28 00:01:24,350 --> 00:01:27,750 and then let's actually implement this geospatial data. 29 00:01:27,750 --> 00:01:32,330 And MongoDB uses a special data format called GeoJSON. 30 00:01:32,330 --> 00:01:33,663 All right, so. 31 00:01:35,080 --> 00:01:39,043 GeoJSON, in order to specify geospatial data. 32 00:01:40,370 --> 00:01:43,180 And now, how does this actually work? 33 00:01:43,180 --> 00:01:46,130 Well, this object that we specified here 34 00:01:46,130 --> 00:01:49,650 is actually, this time, not for the schema type options 35 00:01:49,650 --> 00:01:51,963 as we have it, for example, up here. 36 00:01:52,830 --> 00:01:55,970 So this object here is for the schema type options. 37 00:01:55,970 --> 00:01:57,190 Remember that? 38 00:01:57,190 --> 00:01:59,960 But now, this object here is actually really 39 00:01:59,960 --> 00:02:01,300 an embedded object. 40 00:02:01,300 --> 00:02:02,830 And so inside this object, 41 00:02:02,830 --> 00:02:05,310 we can specify a couple of properties. 42 00:02:05,310 --> 00:02:08,520 All right, and in order for this object to be recognized 43 00:02:08,520 --> 00:02:11,720 as geospatial JSON, we need the type 44 00:02:11,720 --> 00:02:14,133 and the coordinates properties, all right. 45 00:02:15,210 --> 00:02:17,340 So we need type and we need 46 00:02:19,270 --> 00:02:21,060 coordinates, all right. 47 00:02:21,060 --> 00:02:22,970 And so now, each of these fields here, 48 00:02:22,970 --> 00:02:26,490 so basically, each of these sub-fields is then gonna get 49 00:02:26,490 --> 00:02:28,470 its own schema type options. 50 00:02:28,470 --> 00:02:31,830 All right, so basically here, it's a bit nested, 51 00:02:31,830 --> 00:02:33,453 so we have one level deeper. 52 00:02:34,870 --> 00:02:37,910 Okay, so we have the type schema type options 53 00:02:37,910 --> 00:02:41,040 and then we also need schema type options for coordinates 54 00:02:41,040 --> 00:02:44,660 and again, just like we have up here in these other fields 55 00:02:44,660 --> 00:02:47,800 with the difference that these are now sub-fields. 56 00:02:47,800 --> 00:02:50,873 So type needs the type of string, 57 00:02:52,070 --> 00:02:52,903 all right, 58 00:02:53,940 --> 00:02:57,880 and the default should be point. 59 00:02:57,880 --> 00:03:00,240 So remember how I said that we can specify 60 00:03:00,240 --> 00:03:02,647 multiple geometries in MongoDB? 61 00:03:02,647 --> 00:03:04,670 And the default one is always point. 62 00:03:04,670 --> 00:03:07,990 But again, we could also specify polygons or lines 63 00:03:07,990 --> 00:03:10,380 or other geometries like that. 64 00:03:10,380 --> 00:03:12,310 But in this case, for the startLocation, 65 00:03:12,310 --> 00:03:13,910 it really should be point. 66 00:03:13,910 --> 00:03:17,060 And so let's actually make that the only possible option 67 00:03:17,060 --> 00:03:19,283 by specifying the enum, 68 00:03:20,300 --> 00:03:22,703 so the enumeration property. 69 00:03:23,700 --> 00:03:25,800 So remember, we can specify an array 70 00:03:25,800 --> 00:03:28,900 of all the possible options that this field can take 71 00:03:28,900 --> 00:03:33,290 and so in this case, we only want it to be point, all right. 72 00:03:33,290 --> 00:03:35,580 So we did that somewhere up here. 73 00:03:37,810 --> 00:03:39,750 Yeah, right here with the difficulty 74 00:03:39,750 --> 00:03:44,230 where we said it can only be either medium or difficult. 75 00:03:44,230 --> 00:03:46,800 Right, and so here, we're doing the same thing, 76 00:03:46,800 --> 00:03:50,380 but only with one option, okay. 77 00:03:50,380 --> 00:03:52,620 So we need to define the type, remember, 78 00:03:52,620 --> 00:03:55,340 and also, an array of coordinates. 79 00:03:55,340 --> 00:03:59,370 Okay, and so we do that by saying number, 80 00:03:59,370 --> 00:04:02,470 but then in these curly brackets. 81 00:04:02,470 --> 00:04:05,020 Okay, and so that basically means that we expect 82 00:04:05,020 --> 00:04:07,150 an array of numbers 83 00:04:07,150 --> 00:04:09,910 and this array, as the name says, is the coordinates 84 00:04:09,910 --> 00:04:12,420 of the point with the longitude first 85 00:04:12,420 --> 00:04:14,450 and only second, the latitude. 86 00:04:14,450 --> 00:04:16,290 And so that's a bit counterintuitive 87 00:04:16,290 --> 00:04:18,540 because usually it works the other way around. 88 00:04:18,540 --> 00:04:21,530 But in GeoJSON, that's just how it works. 89 00:04:21,530 --> 00:04:24,240 So if you were to go, for example, to Google Maps 90 00:04:24,240 --> 00:04:25,930 in order to get your coordinates, 91 00:04:25,930 --> 00:04:29,560 then you will see first the latitude and then the longitude. 92 00:04:29,560 --> 00:04:31,823 Let's actually take a look at that. 93 00:04:33,500 --> 00:04:34,333 So 94 00:04:36,070 --> 00:04:37,273 open up Maps here. 95 00:04:38,570 --> 00:04:40,423 Let's say New York. 96 00:04:41,860 --> 00:04:44,543 Okay, and if we now click somewhere here, 97 00:04:46,030 --> 00:04:48,520 say here, then we get the coordinates 98 00:04:48,520 --> 00:04:51,880 first with the latitude and second, the longitude. 99 00:04:51,880 --> 00:04:54,960 All right, and in case your not familiar with that, 100 00:04:54,960 --> 00:04:56,993 let's just zoom out a little bit here, 101 00:04:58,250 --> 00:05:00,760 so we can see the entire earth. 102 00:05:00,760 --> 00:05:01,880 Wow, what's that? 103 00:05:02,757 --> 00:05:04,220 Okay. 104 00:05:04,220 --> 00:05:07,610 So the latitude is basically the horizontal position 105 00:05:07,610 --> 00:05:10,680 measured in degrees starting from the equator, 106 00:05:10,680 --> 00:05:13,280 so from here to up here. 107 00:05:13,280 --> 00:05:15,350 So in the equator, it's zero degrees 108 00:05:15,350 --> 00:05:18,770 and up here, in the North Pole, it's 90 degrees. 109 00:05:18,770 --> 00:05:19,980 And then the longitude 110 00:05:19,980 --> 00:05:23,150 is just the same thing, but vertically. 111 00:05:23,150 --> 00:05:27,440 So it's the position starting from a meridian, 112 00:05:27,440 --> 00:05:30,850 which passes somewhere here, okay. 113 00:05:30,850 --> 00:05:33,410 And so the longitude is the vertical position 114 00:05:33,410 --> 00:05:34,960 starting from here. 115 00:05:34,960 --> 00:05:37,783 And so that's why this point here that we got, 116 00:05:40,150 --> 00:05:41,183 somewhere here, 117 00:05:42,070 --> 00:05:45,400 somewhere here in Manhattan, has a latitude of 40 118 00:05:45,400 --> 00:05:48,380 and a longitude of -73. 119 00:05:48,380 --> 00:05:49,650 All right. 120 00:05:49,650 --> 00:05:54,540 But anyway, let's go back to our code here, of course, 121 00:05:54,540 --> 00:05:58,653 because I also want to specify a property for the address. 122 00:06:01,060 --> 00:06:05,600 So add another string and then also, a description 123 00:06:05,600 --> 00:06:07,457 of this startLocation 124 00:06:08,890 --> 00:06:12,070 and that again, as a string, all right. 125 00:06:12,070 --> 00:06:14,060 And we're not gonna say that any of these fields 126 00:06:14,060 --> 00:06:16,930 should be required because we want to be allowed 127 00:06:16,930 --> 00:06:19,490 to leave the startLocation blank. 128 00:06:19,490 --> 00:06:23,520 Okay, so again, in order to specify geospatial data 129 00:06:23,520 --> 00:06:27,500 with MongoDB, we basically need to create a new object 130 00:06:27,500 --> 00:06:28,980 such as we did here. 131 00:06:28,980 --> 00:06:32,290 And that object then needs to have at least two field names. 132 00:06:32,290 --> 00:06:35,140 So coordinates has this array of numbers 133 00:06:35,140 --> 00:06:38,010 and then the type, which should be of type string 134 00:06:38,010 --> 00:06:40,770 and should be either point or some other 135 00:06:40,770 --> 00:06:43,670 of these other geometries that I just told you about before. 136 00:06:43,670 --> 00:06:47,260 Okay, and we can then of course add some more fields 137 00:06:47,260 --> 00:06:50,840 to this object such as we did here, all right. 138 00:06:50,840 --> 00:06:53,430 Now, remember how in the last lecture we said 139 00:06:53,430 --> 00:06:55,920 how we were gonna embed all the locations 140 00:06:55,920 --> 00:06:57,650 into the tour documents? 141 00:06:57,650 --> 00:07:00,600 But right now, the startLocation here is not really 142 00:07:00,600 --> 00:07:02,340 a document itself. 143 00:07:02,340 --> 00:07:03,590 It's really just an object 144 00:07:03,590 --> 00:07:06,070 describing a certain point on earth. 145 00:07:06,070 --> 00:07:08,520 But in order to really create new documents 146 00:07:08,520 --> 00:07:11,250 and then embed them into another document, 147 00:07:11,250 --> 00:07:14,220 we actually need to create an array, all right. 148 00:07:14,220 --> 00:07:17,350 So it's actually very similar to what we already have here, 149 00:07:17,350 --> 00:07:19,050 but it needs to be an array. 150 00:07:19,050 --> 00:07:21,743 And so that's what we're gonna do with our locations. 151 00:07:25,190 --> 00:07:28,730 So locations, and now, I'm creating an array. 152 00:07:28,730 --> 00:07:31,740 And then in this array is where I'm gonna specify 153 00:07:31,740 --> 00:07:35,640 the object such as I did it before in startLocation. 154 00:07:35,640 --> 00:07:38,140 Okay, and you will see what this will look like 155 00:07:38,140 --> 00:07:41,430 in a second in the Compass application. 156 00:07:41,430 --> 00:07:44,693 Okay, and so now, it's actually quite the same as before. 157 00:07:47,530 --> 00:07:50,220 So the type, remember, for geospatial data 158 00:07:50,220 --> 00:07:51,263 needs to be string. 159 00:07:52,640 --> 00:07:54,383 The default needs to be point. 160 00:07:57,540 --> 00:08:01,323 And also, it cannot be anything but point, okay. 161 00:08:02,950 --> 00:08:03,783 So point. 162 00:08:05,610 --> 00:08:09,010 Again, we need the coordinates as an array of numbers 163 00:08:11,000 --> 00:08:13,470 and also, for all of these other locations, 164 00:08:13,470 --> 00:08:16,573 we still want to specify an address and a description. 165 00:08:24,550 --> 00:08:28,153 So string and here, I also want to specify the date, 166 00:08:29,920 --> 00:08:31,250 so as a number. 167 00:08:31,250 --> 00:08:34,080 And this date will basically be the day of the tour 168 00:08:34,080 --> 00:08:37,590 in which people will go to this location. 169 00:08:37,590 --> 00:08:39,460 Now, if we wanted to make it simpler, 170 00:08:39,460 --> 00:08:42,530 we could delete the startLocation all together 171 00:08:42,530 --> 00:08:44,740 and then simply define the first location 172 00:08:44,740 --> 00:08:48,300 as the startLocation and set it to day number zero. 173 00:08:48,300 --> 00:08:50,960 All right, but I decided it's nice to also have 174 00:08:50,960 --> 00:08:53,970 the startLocation as a separate field. 175 00:08:53,970 --> 00:08:57,390 Okay, so this is how you create embedded documents. 176 00:08:57,390 --> 00:09:01,630 Remember we always need to use this array, okay. 177 00:09:01,630 --> 00:09:04,800 And so by specifying basically an array of objects, 178 00:09:04,800 --> 00:09:07,220 this will then create brand new documents 179 00:09:07,220 --> 00:09:09,150 inside of the parent document, 180 00:09:09,150 --> 00:09:11,000 which is, in this case, the tour. 181 00:09:11,000 --> 00:09:13,790 All right, now, in order to create some locations, 182 00:09:13,790 --> 00:09:17,080 I'm actually going to import all our original data. 183 00:09:17,080 --> 00:09:19,570 All right, so instead of creating new tours, 184 00:09:19,570 --> 00:09:21,040 I will delete the ones we have 185 00:09:21,040 --> 00:09:23,610 and then import the complete data. 186 00:09:23,610 --> 00:09:25,283 So here, in dev-data, 187 00:09:26,390 --> 00:09:29,313 remember that before we imported tours-simple. 188 00:09:30,350 --> 00:09:32,820 Okay, so this kind of data here. 189 00:09:32,820 --> 00:09:36,020 But we also have tours and this then actually has 190 00:09:37,818 --> 00:09:41,272 the locations and the startLocation. 191 00:09:41,272 --> 00:09:43,890 So I'm not sure where that startLocation is. 192 00:09:43,890 --> 00:09:45,580 Ah, here it is. 193 00:09:45,580 --> 00:09:48,177 So this is how we specify the startLocation 194 00:09:48,177 --> 00:09:51,150 and you see that we have type set to point. 195 00:09:51,150 --> 00:09:53,520 We have our array of coordinates. 196 00:09:53,520 --> 00:09:56,660 And then of course, we have description and address. 197 00:09:56,660 --> 00:10:01,000 And then down here, in the location, we have an array, 198 00:10:01,000 --> 00:10:03,820 which then contains one object for each location. 199 00:10:03,820 --> 00:10:07,550 And you see that actually each of them gets their own id. 200 00:10:07,550 --> 00:10:09,260 And so these really are documents 201 00:10:09,260 --> 00:10:11,360 and not just simple objects. 202 00:10:11,360 --> 00:10:15,480 All right, so let's go here to our import-dev-data 203 00:10:15,480 --> 00:10:18,673 and replace this here just with tours. 204 00:10:20,700 --> 00:10:25,000 Give it a save and then remember, we need first to delete 205 00:10:25,000 --> 00:10:26,133 and then to import. 206 00:10:27,060 --> 00:10:29,063 So going to another console here. 207 00:10:30,140 --> 00:10:32,213 We now want to run node, 208 00:10:33,860 --> 00:10:36,750 dev-data and then import, 209 00:10:36,750 --> 00:10:40,063 so actually, in the data folder, and then import. 210 00:10:41,520 --> 00:10:46,520 And start by deleting and let's wait for it and indeed, 211 00:10:47,790 --> 00:10:48,793 and now, import. 212 00:10:53,500 --> 00:10:55,060 And here we go. 213 00:10:55,060 --> 00:10:56,763 And let's check out Compass now. 214 00:10:58,680 --> 00:10:59,953 Come to our tours. 215 00:11:01,830 --> 00:11:05,080 And so indeed, you now see the startLocation here 216 00:11:05,080 --> 00:11:06,660 as an object. 217 00:11:06,660 --> 00:11:08,500 Okay, and when you open that up, 218 00:11:08,500 --> 00:11:10,730 you see all the data that I just showed you before 219 00:11:10,730 --> 00:11:12,390 in the JSON file. 220 00:11:12,390 --> 00:11:15,140 Right, then here, also, the locations, 221 00:11:15,140 --> 00:11:17,200 which you see as an array, so type array. 222 00:11:17,200 --> 00:11:21,010 And then in there, we have all of these objects 223 00:11:21,010 --> 00:11:25,000 and each object then also has its object id. 224 00:11:25,000 --> 00:11:28,680 Okay, and so again, this is proof that we have now created, 225 00:11:28,680 --> 00:11:31,450 embedded, or de-normalized datasets, 226 00:11:31,450 --> 00:11:34,980 so datasets that have a really close relationship 227 00:11:34,980 --> 00:11:36,810 with the tours data. 228 00:11:36,810 --> 00:11:39,450 And so that's why we chose to really make it part 229 00:11:39,450 --> 00:11:42,780 of the tours instead of creating its own collection 230 00:11:42,780 --> 00:11:45,060 just for locations, right. 231 00:11:45,060 --> 00:11:48,073 So we will actually use this a bit later in this section 232 00:11:48,073 --> 00:11:52,570 once we start to create some special geospatial queries. 233 00:11:52,570 --> 00:11:55,120 Okay, and with geospatial queries, 234 00:11:55,120 --> 00:11:57,300 we can do really amazing stuff 235 00:11:57,300 --> 00:12:00,570 like finding locations data closest to certain points 236 00:12:00,570 --> 00:12:03,220 or find all locations inside a certain radius 237 00:12:03,220 --> 00:12:04,790 or a certain sphere. 238 00:12:04,790 --> 00:12:09,260 And really, the possibilities are absolutely endless, okay. 239 00:12:09,260 --> 00:12:11,760 So we will just see some applications of this 240 00:12:11,760 --> 00:12:13,670 by the end of this section 241 00:12:13,670 --> 00:12:16,090 because for now, I really just wanted to show you 242 00:12:16,090 --> 00:12:20,183 how we can create embedded datasets just as we just did.