1 00:00:02,130 --> 00:00:08,240 Now our query failed because we had no geospatial index. We need one for the near query here, 2 00:00:08,400 --> 00:00:11,070 so how can we add such an index, 3 00:00:11,070 --> 00:00:12,620 it's pretty straightforward. 4 00:00:12,660 --> 00:00:19,920 Just as with other indexes, we use create index to add such index and then, we add the index on the location 5 00:00:19,920 --> 00:00:23,080 field because that is where we store our geo data 6 00:00:23,100 --> 00:00:27,040 in this example, again if you named that field differently, 7 00:00:27,090 --> 00:00:28,560 definitely use your name here, 8 00:00:28,560 --> 00:00:29,540 you have to. 9 00:00:30,000 --> 00:00:33,630 And then the type of the index is now not 1 or -1, 10 00:00:33,630 --> 00:00:35,970 we don't sort it ascending or descending, 11 00:00:36,090 --> 00:00:39,150 it's also not text because it's not a text index, 12 00:00:39,150 --> 00:00:43,870 it's a special 2D sphere index, written like this, 13 00:00:43,870 --> 00:00:49,020 whoops, not like this, like this, 2d sphere in quotation marks. 14 00:00:49,020 --> 00:00:53,180 With this, we created a geospatial index on the location field 15 00:00:53,190 --> 00:00:59,430 and now if we repeat that query from before, it does succeed and we can also pretty print that and we 16 00:00:59,430 --> 00:01:02,520 see that it found this one point. 17 00:01:02,650 --> 00:01:06,630 Now it found this one point but that doesn't really tell us too much, 18 00:01:06,630 --> 00:01:08,840 it's near the location we added 19 00:01:08,970 --> 00:01:11,210 but how is near defined? 20 00:01:11,670 --> 00:01:14,740 Well near doesn't make much sense unless we restrict it, 21 00:01:15,000 --> 00:01:21,990 so typically what you would do is you would not just pass a geometry to near as we're doing it here but 22 00:01:21,990 --> 00:01:28,620 you would also pass another argument and define a max and maybe also a min distance. 23 00:01:28,620 --> 00:01:32,070 Now max distance is simply a value in meters here, 24 00:01:32,070 --> 00:01:36,810 so if I define like 30 meters, I should actually not find anything, 25 00:01:36,870 --> 00:01:41,660 we can also add the min distance and say it should at least be 10 meters away. 26 00:01:42,030 --> 00:01:47,850 So if I add this, I actually don't find anything and that makes sense because if we have a look at Google 27 00:01:47,850 --> 00:01:52,520 Maps, this is the place, the California Academy of Sciences 28 00:01:52,540 --> 00:01:53,780 and this is where we are 29 00:01:54,050 --> 00:01:56,390 and that seems to be a bit further away, 30 00:01:56,570 --> 00:02:01,430 we can actually measure the distance by right clicking onto the map and measuring a distance and then 31 00:02:01,430 --> 00:02:08,640 if I click here, I can measure the distance to the California Academy of Sciences and it seems to be 32 00:02:08,640 --> 00:02:13,030 around 435,36 meters. 33 00:02:13,260 --> 00:02:18,310 Let's validate this by repeating the query and I'll set the max distance to 400, 34 00:02:18,360 --> 00:02:26,750 so this still should not suffice and it does not. Now if I set it to 440, looks like a little 35 00:02:26,750 --> 00:02:30,230 bit off of my place, so let's set it to 500 36 00:02:30,380 --> 00:02:32,070 and now we find the place, 37 00:02:32,090 --> 00:02:37,440 so now we are within 10 and 500 meters and therefore, we find our favorite place. 38 00:02:37,520 --> 00:02:44,750 And this of course is a great query because it allows us to find out which places are near us in a certain 39 00:02:44,750 --> 00:02:50,070 radius which of course is off my question you want to answer in an application.