1 00:00:02,110 --> 00:00:10,570 Now to conclude the geospatial queries we can run, let's find all places in a certain radius around 2 00:00:10,570 --> 00:00:11,220 the user. 3 00:00:11,260 --> 00:00:14,540 We kind of did this before with the near command, 4 00:00:14,740 --> 00:00:17,500 now I also want to give you a different way. 5 00:00:17,740 --> 00:00:20,430 The near command also sorted the results, 6 00:00:20,440 --> 00:00:21,730 that is the important difference 7 00:00:21,730 --> 00:00:23,010 and I'll come back to that, 8 00:00:23,240 --> 00:00:30,600 let's now place the user here and let's check for a certain radius and all the points in that radius. 9 00:00:30,850 --> 00:00:33,510 So for that, we'll need the coordinates of the user of course 10 00:00:33,850 --> 00:00:40,030 and I'll write a new query, again against my places collection not the areas collection because the 11 00:00:40,030 --> 00:00:46,060 places collection is the collection with these four places we added, right. 12 00:00:46,120 --> 00:00:53,200 So this is the collection I'm interested in now and there, I want to find all elements in an unsorted 13 00:00:53,260 --> 00:00:56,250 order that are within a certain radius. 14 00:00:56,590 --> 00:01:00,750 So here the location field is the field which stores the coordinates, 15 00:01:00,760 --> 00:01:05,360 so this is the field which I will now use with the geoWithin 16 00:01:05,380 --> 00:01:12,520 operator again. Why geoWithin and not geoIntersects? Because now I want to find all places that are within 17 00:01:12,820 --> 00:01:16,780 a place or an area which I'll define here in the query, 18 00:01:16,780 --> 00:01:18,020 previously we had the opposite, 19 00:01:18,040 --> 00:01:24,070 we had a point in the query and wanted to find an area that surrounds the point. 20 00:01:24,100 --> 00:01:32,180 So geoWithin normally takes a geometry operator which describes a geo json object but there is 21 00:01:32,180 --> 00:01:34,680 a special operator you can use in 22 00:01:34,680 --> 00:01:35,360 mongodb 23 00:01:35,570 --> 00:01:41,360 and that is the centerSphere operator, $centerSphere. 24 00:01:41,450 --> 00:01:46,900 This is a helpful operator that allows you to quickly get a circle around a point, 25 00:01:46,940 --> 00:01:53,780 so essentially it use a radius and a center and gives you the whole circle around that center therefore. 26 00:01:54,530 --> 00:02:01,280 centerSphere takes an array as a value and that array has two elements, 27 00:02:01,280 --> 00:02:08,120 the first element is another array which holds your coordinates, again longitude and then latitude 28 00:02:08,330 --> 00:02:14,180 and with coordinates, I mean the coordinates of the center of the circle you want to draw. 29 00:02:14,660 --> 00:02:17,970 The second element in this array is the radius then 30 00:02:18,290 --> 00:02:25,580 and now this radius needs to be translated manually from meters or miles to radians. 31 00:02:25,580 --> 00:02:31,190 Now this conversion is relatively easy to do and you'll find an article in the mongodb docs 32 00:02:31,190 --> 00:02:31,850 too, 33 00:02:32,240 --> 00:02:38,150 you'll find an article which mentions how to translate miles to radians or kilometers. 34 00:02:38,150 --> 00:02:39,920 Now I will work with kilometers here, 35 00:02:39,920 --> 00:02:42,450 so this is the value which is interesting to me 36 00:02:42,950 --> 00:02:48,270 and now I want to look in a radius of let's say one kilometer, 37 00:02:48,560 --> 00:02:54,360 so one divided by this value without the comma is my radius here. 38 00:02:55,490 --> 00:03:02,120 If I hit enter, I find two points, let me pretty print this, I find the Conservatory of Flowers and the 39 00:03:02,120 --> 00:03:03,530 Golden Gate Tennis Park, 40 00:03:03,620 --> 00:03:07,300 what I don't find is to California Academy of Sciences, 41 00:03:07,460 --> 00:03:12,640 let's quickly measure the distance of this point to this point. 42 00:03:12,710 --> 00:03:15,300 Now it looks like this should be in there, 43 00:03:15,320 --> 00:03:21,910 so my assumption would be that it's simply not in there because I have the wrong coordinates stored. 44 00:03:21,950 --> 00:03:28,430 Let's quickly look into our database for the California Academy of Sciences and indeed, the coordinates 45 00:03:28,430 --> 00:03:29,700 here don't match mine, 46 00:03:29,750 --> 00:03:34,820 that is because previously I didn't have my browser perfectly centered on them. 47 00:03:34,820 --> 00:03:39,130 So let's quickly update that to be really sure that our query here works correctly. 48 00:03:39,380 --> 00:03:45,530 So I'll update by simply searching for that objectId here, places and you know how to update, 49 00:03:45,650 --> 00:03:46,770 I hope you still know it, 50 00:03:47,710 --> 00:03:53,230 first of all you filter for _id being equal to my objectId here 51 00:03:53,240 --> 00:03:59,010 and the second value is then the value where you describe the update with $set. 52 00:03:59,050 --> 00:04:03,200 I will set my location to a new value, 53 00:04:03,370 --> 00:04:10,900 the type will still be a point, that hasn't changed but the coordinates, that will now be a new array 54 00:04:10,900 --> 00:04:12,450 with new values, 55 00:04:12,640 --> 00:04:17,910 first the longitude here and second, the latitude. 56 00:04:18,110 --> 00:04:24,020 So let's grab this and this is now the exact point, the exact location of that institute. 57 00:04:24,110 --> 00:04:28,570 So with this updated, let's run our query again and 58 00:04:28,670 --> 00:04:32,320 now the California Academy of Sciences is included too 59 00:04:32,510 --> 00:04:38,630 but please note that Nopa is not included and that makes sense because Nopa should definitely not 60 00:04:38,630 --> 00:04:40,900 be inside of our circle. 61 00:04:41,270 --> 00:04:46,060 If I measure the distance of this point to this point, it's above two kilometers, 62 00:04:46,130 --> 00:04:48,630 so it makes sense that this is not included. 63 00:04:49,190 --> 00:04:52,660 So this is how we can run such queries as well, 64 00:04:52,670 --> 00:04:59,330 the important difference to the near query which also allowed us to find places within a certain min 65 00:04:59,360 --> 00:05:02,120 and max distance is that with this query, 66 00:05:02,150 --> 00:05:07,100 we get back an unsorted list, this keeps the order of the elements in the database 67 00:05:07,190 --> 00:05:14,570 or we could of course manually sort them with sort method. The near method did give us the elements in 68 00:05:14,570 --> 00:05:18,130 a certain radius and did sort them by proximity 69 00:05:18,320 --> 00:05:23,780 and that really depends on your requirements, what you need, do you need sorted results with the nearest 70 00:05:23,780 --> 00:05:24,860 result first 71 00:05:24,860 --> 00:05:30,410 or do you need unsorted results and you just want to get a list of the elements in an area in general? 72 00:05:30,800 --> 00:05:32,840 This is what geoWithin can do, 73 00:05:32,840 --> 00:05:35,960 near is otherwise your solution for the sorted list.