Now another typical use case would be the opposite, that you have an application where you want to
find out whether the user is in a certain area,
so we don't want to find all places in an area but we want to store a couple of different areas potentially
in the database,
let's say the neighborhoods of a city and then your user sends some coordinates because he located
himself and you want to find out in which neighborhood the user is.
So essentially the same query as before, just the other way around.
Now to keep things simple, I will work with the same area but I will now store it in the database and
I then want to find out if a user let's say is here, if this user is inside this area and we'll also
check this with a point outside of the area then.
So let's first of all store our nice polygon which we used here and therefore, I will just copy this geo
json object, let's store this in the database.
For that I'll add a new collection, areas,
of course you can name this however you want
where I will insert one area and that area will have a name of let's say Golden Gate Park and then I'll
add a field,
I could name it
polygon but I'll just name it area,
that name is totally up to you,
the value will be my geo json polygon object with the coordinates I stored before.
So this added this object and we can have a quick look into our areas to confirm that we have all the
coordinates for the corners here,
first and last coordinate pair have to match. Ok now, so we got that place in here,
now with that place added, let's find out if this user is inside of that area.
Now how can we do that?
First of all we need to create an index again,
so let's run db areas create index and then the index should be created on the area key which I added
and should be a 2D sphere index of course.
So with the index added, we can write our query and there we want to find out basically if the user is
inside of this area,
we can do this by checking for geoIntersects.
Now what does geoIntersects do? geoWithin allowed us to find out which places are inside of an area,
geoIntersects simply returns all areas that have a common point or a common area.
Now why don't I use geoWithin because I can't find areas that are within the point where the user is,
the user is just a point
and the area will never be within that point.
The other way around is what I want to see and I get this by simply checking
does this point intersect with the area?
If the answer is yes,
so if I do get an area returned, I know that the user is well in that area which is returned because
that is what I'm trying to find out, in which area is the user.
So geoIntersects is what I need to use there and as before, we can simply pass a geometry here with
$geometry which is now again a geo json object,
this is now just a point, the coordinates of the user and the coordinates here are a simple array of
the longitude and the latitude.
So let's grab the longitude of that user here,
like this and let's grab the latitude,
so this first value here. If I hit enter, I get an error because of course geoIntersects is generally
all right
but the field for which I'm looking is the area field and then this geoIntersects operator gets used on
the area field,
so my mistake. Just as before,
this doesn't change,
we need to let mongodb know hey in which field is the data I want to compare and that of course is the
area field in our database because the area field is where do I store the area I'm looking for
and then I use geoIntersects.
So with this fixed, now we do get a place here and that is looking good.
I'm getting the only place I have in that database but I'm getting the place with which my point intersects
and if my point would be in multiple areas, I would of course get all these areas which intersect with
my point.
You can also not just intersect points with areas, you could also intersect areas with areas and see
if two areas touch. Let's say we want to find out which neighborhood contains the Golden Gate Park, the Golden
Gate Park could be a polygon and the neighborhood around it could be one too and you could find out
if they intersect and if yes, which neighborhood intersects with the Golden Gate Park. Here we just looked
at a point
and now to complete this, let's also look at a point which is not inside the Golden Gate Park.
So I'll add a new point outside of it, here and definitely feel free to pause the video at this point and do this
on your own,
I will do it together with you.
Here are the coordinates
and in the end, we just have to repeat the last query, this time add the new coordinates,
so the new coordinates of this new point for the point we want to see if it intersects with the Golden
Gate Park and hit enter and we don't get back a result because in our areas collection, we have no areas
that would contain this point.
This is the answer this gives us.