1 00:00:02,120 --> 00:00:11,180 Let's run our first geo query. For that I'll work with my places and I want to find places that are near 2 00:00:11,510 --> 00:00:13,280 my current location. 3 00:00:13,280 --> 00:00:15,340 Now what is my location? Now 4 00:00:15,350 --> 00:00:16,340 always keep in mind 5 00:00:16,370 --> 00:00:21,130 that of course rarely we'll work with the shell as we do, most of the time, 6 00:00:21,170 --> 00:00:26,870 you'll write an application that does what we are now doing with the shell or then it will do that with 7 00:00:26,870 --> 00:00:28,150 its driver of course. 8 00:00:28,370 --> 00:00:32,880 So therefore you might have a web application where users can locate themselves, 9 00:00:33,020 --> 00:00:38,840 you can do that through some web APIs or you have a mobile app where users can locate themselves. Such 10 00:00:38,850 --> 00:00:45,100 location APIs will always return coordinates in the form of latitude and longitude, 11 00:00:45,110 --> 00:00:46,790 this is the standard format. 12 00:00:46,940 --> 00:00:52,250 So your application will give you some latitude and longitude data for whatever your user did, 13 00:00:52,250 --> 00:00:54,020 for example locate himself. 14 00:00:54,020 --> 00:00:55,660 Now let's fake that part, 15 00:00:55,730 --> 00:01:02,160 so let's simply click somewhere, here for example and now I marked a place where we also see the coordinates. 16 00:01:02,180 --> 00:01:05,610 If I click on that, I can also copy them from there. Again, 17 00:01:05,630 --> 00:01:11,070 the second coordinate is the longitude, first one is the latitude. So this is the location we assume 18 00:01:11,070 --> 00:01:12,900 we're getting from our app, 19 00:01:12,920 --> 00:01:19,130 so how can we now use that location to find out if this place here is near our location and what does 20 00:01:19,250 --> 00:01:20,580 near define? 21 00:01:20,810 --> 00:01:23,870 Well let's see that step by step. In find, 22 00:01:23,930 --> 00:01:29,810 I definitely want to have a look at my location key and if you named that differently, you need to name it 23 00:01:29,810 --> 00:01:30,620 differently here, 24 00:01:30,620 --> 00:01:32,230 this is not a reserved keyword, 25 00:01:32,240 --> 00:01:34,140 this is simply referring to our key here 26 00:01:35,500 --> 00:01:41,800 and then here, the value is a document where I use $near, 27 00:01:41,830 --> 00:01:46,210 $near is one of the operators provided by mongodb for working with 28 00:01:46,220 --> 00:01:47,790 geospatial data. 29 00:01:48,310 --> 00:01:52,850 $near requires another document as a value and there, 30 00:01:53,080 --> 00:01:57,370 you can now define a geometry for which you want to check if it's near, 31 00:01:57,400 --> 00:02:01,250 so $geometry. That again is a document 32 00:02:01,450 --> 00:02:08,680 and this now again describes a geo json object, so we could check here if a point we add here is close 33 00:02:08,680 --> 00:02:11,020 to our point and that's exactly what I'll do. 34 00:02:11,260 --> 00:02:17,830 So since we have to pass in some geo json here, I can add a type and that type will also be a point, 35 00:02:17,830 --> 00:02:24,460 we'll work with different types later by the way and I will pass my coordinates, just as before because 36 00:02:24,520 --> 00:02:26,010 this has to be geo json, 37 00:02:26,200 --> 00:02:31,570 we have to have a type here which is one of the valid types and we have to have coordinates here. 38 00:02:31,570 --> 00:02:35,890 And you therefore of course also know which format coordinates should be in, correct 39 00:02:35,890 --> 00:02:38,740 it should be an array where the first value is the longitude, 40 00:02:38,950 --> 00:02:40,960 second value is the latitude. 41 00:02:41,260 --> 00:02:48,010 So let's paste in that longitude we grab from Google Maps and let's then also grab the latitude 42 00:02:48,550 --> 00:02:51,540 and let's paste this as a second value. 43 00:02:51,580 --> 00:02:58,180 Now if I hit enter, I get an error and the error is telling me that it's unable to find an index for 44 00:02:58,190 --> 00:03:04,580 the geoNear query and geoNear is simply the behind the scenes name of our near query. 45 00:03:04,870 --> 00:03:09,170 So for this, we will need a geospatial index for this query to run. 46 00:03:09,190 --> 00:03:15,280 Not all geospatial queries require index but they all just as with other indexes will most likely 47 00:03:15,280 --> 00:03:17,300 benefit from having such an index, 48 00:03:17,380 --> 00:03:20,380 so let's add a geospatial index in the next lecture.