1 00:00:02,080 --> 00:00:07,960 So I want to find all places inside Golden Gate Park, so Nopa should not be included. 2 00:00:08,140 --> 00:00:14,560 Now to find them, what I will do because that makes it a bit easier to work with that is I'll go into 3 00:00:14,820 --> 00:00:22,870 the your places tab and create a new map there and here, it will be a bit easier to navigate around and 4 00:00:22,870 --> 00:00:28,680 find coordinates and also show which place the coordinates are for. 5 00:00:28,690 --> 00:00:35,080 So in here, I want to basically well draw a polygon around my park, 6 00:00:35,110 --> 00:00:36,430 so let's start here, 7 00:00:36,430 --> 00:00:40,250 doesn't have to be perfect but somewhere here will be my first place. 8 00:00:40,300 --> 00:00:48,010 So what I will do is I will add a marker here, I'll delete it immediately but here, we get the exact coordinates. 9 00:00:48,040 --> 00:00:49,650 So let's take that 10 00:00:50,040 --> 00:00:52,750 and let's use that in a future query. 11 00:00:52,780 --> 00:00:55,640 So first of all to make this a bit easier, 12 00:00:55,880 --> 00:00:59,570 I'll create four variables, 13 00:00:59,800 --> 00:01:03,080 p1 let's say which essentially will just store these coordinates. 14 00:01:03,080 --> 00:01:05,350 So here I have the longitude of the first place, 15 00:01:05,450 --> 00:01:12,260 the first corner of my polygon and the latitude will be my second element and since the mongo shell uses 16 00:01:12,260 --> 00:01:12,770 javascript, 17 00:01:12,770 --> 00:01:14,790 I can just create constants like this, 18 00:01:14,810 --> 00:01:20,440 so this value is now simply stored in P1 and we can always access it under this name. 19 00:01:20,450 --> 00:01:28,100 So with that, let's remove that point here and let's create a second point in the next corner, here 20 00:01:28,400 --> 00:01:32,660 and of course if you already got your coordinates, you can skip this whole exercise and just go with 21 00:01:32,660 --> 00:01:33,160 them. 22 00:01:33,380 --> 00:01:34,620 So this is my next point, 23 00:01:36,530 --> 00:01:37,900 const p2, 24 00:01:38,170 --> 00:01:43,120 this is my longitude, now let me grab my latitude here, 25 00:01:43,120 --> 00:01:47,190 copy that and store this as a second value in this array 26 00:01:48,750 --> 00:01:50,480 and let's get rid of that point 27 00:01:50,480 --> 00:01:56,720 now. Let's move all the way to the left, to the end of the park on the west side, 28 00:01:57,660 --> 00:01:59,330 add a point here, 29 00:01:59,370 --> 00:02:00,450 all right, 30 00:02:00,450 --> 00:02:03,620 grab the longitude of that point like this 31 00:02:06,970 --> 00:02:09,550 and of course, grab the latitude too. 32 00:02:09,580 --> 00:02:14,020 So let's go over there, correct the latitude and store that in this array too. 33 00:02:14,110 --> 00:02:17,550 Now onto the last point and then we're done, 34 00:02:17,560 --> 00:02:18,690 let me go here, 35 00:02:19,780 --> 00:02:28,510 place a marker in the corner, grab these coordinates, 36 00:02:28,730 --> 00:02:35,050 also the latitude of course and now all the points are stored. 37 00:02:35,050 --> 00:02:39,480 So just to show you which area where we now marked, 38 00:02:39,550 --> 00:02:41,940 well essentially we got three points before, 39 00:02:41,980 --> 00:02:43,620 let's start like this. 40 00:02:43,660 --> 00:02:46,370 We have the Conservatory of Flowers roughly, 41 00:02:46,450 --> 00:02:52,530 So I'll just add a point there, we also took the Academy of Sciences so I'll add a point there 42 00:02:53,050 --> 00:02:57,540 and I have a point at the Golden park tennis courts, 43 00:02:57,580 --> 00:03:00,930 so at this point. We also have one at 44 00:03:00,940 --> 00:03:01,590 Nopa, 45 00:03:01,840 --> 00:03:05,830 so let's move over there and let's add a point there, 46 00:03:05,860 --> 00:03:08,530 so these are four points. 47 00:03:08,530 --> 00:03:16,920 Now the area for which I just got the coordinates is essentially the area around the Golden Gate Park 48 00:03:17,320 --> 00:03:20,580 and I can draw a shape around that with that shape tool here. 49 00:03:21,400 --> 00:03:26,590 So this is essentially the area for which I just got the corner coordinates and what I now want to do 50 00:03:26,590 --> 00:03:31,660 is I want to run a query to find all the places that are inside of the shape and the good thing is we 51 00:03:31,660 --> 00:03:35,110 can easily validate this because we can see which points are in there right, 52 00:03:35,110 --> 00:03:36,680 this one should not be in there. 53 00:03:37,000 --> 00:03:38,440 So now that I got these coordinates, 54 00:03:38,440 --> 00:03:41,060 let's finally get to creating that query. 55 00:03:41,440 --> 00:03:47,460 So on the places, let me quickly clear the console, P1 and so on are still available, 56 00:03:47,460 --> 00:03:53,640 so on the places, I will now find something again and again, I will find something with the help of the 57 00:03:53,640 --> 00:03:54,930 location field 58 00:03:55,170 --> 00:03:57,660 but there in the location field, 59 00:03:57,720 --> 00:04:05,860 I now don't want to find some nearest points, so near is wrong but instead I'll use the geoWithin operator, 60 00:04:05,870 --> 00:04:13,790 $geoWithin. This is an operator provided by mongodb that simply can help us find 61 00:04:13,850 --> 00:04:17,920 all elements within a certain shape, within a certain object, 62 00:04:17,960 --> 00:04:24,930 typically something like polygon. Geowithin takes a document as a value 63 00:04:25,040 --> 00:04:30,830 and here we can add a geometry object which is just a geo json object, 64 00:04:30,860 --> 00:04:34,040 so the value here is a geo json object. The type 65 00:04:34,040 --> 00:04:38,130 now is not a point but a polygon, that's important, 66 00:04:38,450 --> 00:04:40,390 coordinates need to be added of course 67 00:04:40,460 --> 00:04:44,020 and now I need to add the coordinates of a polygon. 68 00:04:44,030 --> 00:04:46,100 Now how do I add a polygon? 69 00:04:46,100 --> 00:04:53,510 Obviously a point was just a pair of coordinates, a polygon actually uses a bit more coordinates, to be 70 00:04:53,510 --> 00:04:58,850 precise we need the four corners, for a polygon we add a nested array, 71 00:04:58,850 --> 00:05:06,380 so an array in an array and in that array, we then again add more arrays where each array now describes 72 00:05:06,680 --> 00:05:12,230 one longitude latitude pair of the corners of our polygon. 73 00:05:12,620 --> 00:05:15,720 So I already got these arrays 74 00:05:15,740 --> 00:05:20,500 so to say in my constants P1 to p4 which I created. 75 00:05:20,780 --> 00:05:28,540 So essentially what we added here is p1, P2, p3, P4 to describe the four corners of our polygon. 76 00:05:28,730 --> 00:05:33,700 There is one important thing though, I need the p1 again at the end because a polygon has to end 77 00:05:33,700 --> 00:05:37,150 with a starting point so that it's treated as complete. 78 00:05:37,640 --> 00:05:43,310 Now with that if I hit enter, I do get back results and I can pretty print them 79 00:05:43,400 --> 00:05:49,040 and we see the California Academy of Sciences in there, we see the Golden Gate tennis park and we see 80 00:05:49,040 --> 00:05:55,190 the Conservatory of Flowers, what we don't see is Nopa which makes a lot of sense because it is not 81 00:05:55,190 --> 00:05:56,870 within these boundaries. 82 00:05:56,990 --> 00:06:02,240 So this is how we can also check whether something is in a certain area, with the help of geoWithin 83 00:06:02,550 --> 00:06:08,050 and the geometry operator and then a polygon instead of a point. A polygon 84 00:06:08,060 --> 00:06:15,230 as you learned is in the end created with coordinates that hold a nested array with the different corner coordinates. 85 00:06:15,260 --> 00:06:15,910 Important here is that 86 00:06:15,910 --> 00:06:20,000 you also add the first corner as a last element to close the polygon.