1 00:00:02,130 --> 00:00:07,810 I'm back in the terminal here and I will enter a new database, awesome places by using it 2 00:00:08,000 --> 00:00:14,640 and in this database, I want to add some nice places, essentially locations which I can then query and 3 00:00:14,640 --> 00:00:16,380 we'll do this together right from start, 4 00:00:16,380 --> 00:00:21,840 we'll also add these places right from start so that you can understand how geospatial data needs 5 00:00:21,840 --> 00:00:25,590 to be stored so that you can work with it. In the official docs, 6 00:00:25,590 --> 00:00:32,750 you'll find an article about geo json, how it's structured and which kind of geo json objects mongodb 7 00:00:32,740 --> 00:00:38,100 supports because geo json is not a standard invented by mongodb, 8 00:00:38,250 --> 00:00:41,090 it's actually available outside of mongodb too 9 00:00:41,190 --> 00:00:45,430 and there, you have more objects available that are supported by mongodb 10 00:00:45,540 --> 00:00:46,710 but mongodb supports 11 00:00:46,710 --> 00:00:54,560 all major important objects like points, lines or polygons, as well as some special advanced objects. 12 00:00:54,570 --> 00:00:56,940 Now this article is definitely worth checking out 13 00:00:57,090 --> 00:01:02,880 but the most important thing here is that you understand how a geo json object is created and creating 14 00:01:02,890 --> 00:01:05,400 it is very simple. To work with some data, 15 00:01:05,400 --> 00:01:07,290 I chose the San Francisco area 16 00:01:07,310 --> 00:01:10,890 but of course you can open up Google maps and work with different locations. 17 00:01:10,950 --> 00:01:15,990 I'll use Google Maps to pick some locations so that we also can see something on the map and kind of 18 00:01:16,020 --> 00:01:18,980 validate whether our queries work correctly. 19 00:01:18,990 --> 00:01:27,050 Now let's work with that Golden Gate Park and let's start by adding one simple location in our database, 20 00:01:27,090 --> 00:01:30,240 let's say we are going to pick some awesome places. 21 00:01:30,240 --> 00:01:32,480 I like this park quite a lot actually, 22 00:01:32,490 --> 00:01:37,350 I've been there, I've not been to every place I'm going to pick now but let's simply pick some places 23 00:01:37,350 --> 00:01:43,920 here, like the California Academy of Sciences. Now in Google Maps if you click on a place, you can easily 24 00:01:43,920 --> 00:01:49,280 access the coordinates of that place from inside the URL, you'll find them here. 25 00:01:49,560 --> 00:01:55,120 Now the important thing is the first coordinate until the first comma is the latitude, 26 00:01:55,260 --> 00:02:00,990 the second one is the longitude and you'll need to remember that to store it correctly in mongodb. 27 00:02:01,470 --> 00:02:04,720 Coordinates are always expressed as latitude and longitude 28 00:02:04,740 --> 00:02:11,340 or longitude and latitude pairs because the longitude essentially describes a position on a 29 00:02:11,820 --> 00:02:18,300 vertical axis if you draw it onto our globe, onto the earth globe and the latitude describes a horizontal 30 00:02:18,300 --> 00:02:20,080 axis on the earth globe 31 00:02:20,160 --> 00:02:24,660 and with that coordinate system, we can map any point onto our earth. 32 00:02:24,660 --> 00:02:29,840 So let's work with that data and let's add this as an awesome place to our collection. 33 00:02:29,850 --> 00:02:39,810 So here I'll add a new places collection and I'll insert one new place. Now you insert a normal document, 34 00:02:40,020 --> 00:02:48,240 this doesn't change and I'll give it a name of California Academy of Sciences because that is the name 35 00:02:48,240 --> 00:02:49,400 that we see here 36 00:02:49,890 --> 00:02:54,590 and then I want to add the coordinates in geo json format. Now 37 00:02:54,600 --> 00:02:56,930 there's nothing special about geo json, 38 00:02:56,940 --> 00:03:01,170 you can add any key you want with any name you want, 39 00:03:01,240 --> 00:03:04,860 I'll name it location but you could name it loc, you could name it totally different, 40 00:03:04,860 --> 00:03:06,020 that does not matter, 41 00:03:06,180 --> 00:03:09,220 what matters is the structure of this value. 42 00:03:09,360 --> 00:03:15,520 The value should be an embedded document and in that embedded document, you'll need two pieces of information 43 00:03:15,960 --> 00:03:19,890 and these two pieces of information are also mentioned in the official docs, 44 00:03:19,920 --> 00:03:24,060 you'll need to define the type and you'll need to define the coordinates. 45 00:03:24,060 --> 00:03:27,620 The coordinates are then an array where the first value 46 00:03:27,640 --> 00:03:31,900 and now this is really important, where the first value has to be the longitude 47 00:03:32,130 --> 00:03:35,030 and the second value is the latitude, 48 00:03:35,070 --> 00:03:38,100 so let's add a point exactly in the way we can see it here. 49 00:03:38,370 --> 00:03:41,300 Let's go back and grab our longitude first, 50 00:03:41,310 --> 00:03:43,920 so this second value here on Google Maps, 51 00:03:43,920 --> 00:03:45,150 copy that 52 00:03:45,150 --> 00:03:52,500 and then here, I will first of all add a type and type here is point because I add a single point and 53 00:03:52,500 --> 00:03:57,100 then I add my coordinates and now these key names here are not up to you, 54 00:03:57,120 --> 00:04:02,730 this one has to be named type and it has to be one of the supported types like point and this one has 55 00:04:02,730 --> 00:04:08,550 to be named coordinates and it has to be an array where the first value is now our longitude and the 56 00:04:08,550 --> 00:04:12,990 second value separated with a comma is then our latitude, 57 00:04:13,290 --> 00:04:16,920 so let's grab that latitude here and enter this here. 58 00:04:17,040 --> 00:04:18,710 With this I added this place 59 00:04:18,720 --> 00:04:26,040 and therefore, if I now simply find one, the only place I added thus far, this is now my geo json object, 60 00:04:26,130 --> 00:04:27,470 exactly this 61 00:04:27,530 --> 00:04:32,520 and it is treated as geo json because it fulfills the requirement of having a type which is one 62 00:04:32,520 --> 00:04:37,630 of the supported objects and of having coordinates which is an array where the first value is treated 63 00:04:37,640 --> 00:04:42,110 as the longitude and the second value is treated as a latitude. 64 00:04:42,120 --> 00:04:45,760 Now with that point added, we know how geo json looks like, 65 00:04:45,930 --> 00:04:49,570 let's now find out how we can query that geo json data.