Let's run our first geo query. For that I'll work with my places and I want to find places that are near
my current location.
Now what is my location? Now
always keep in mind
that of course rarely we'll work with the shell as we do, most of the time,
you'll write an application that does what we are now doing with the shell or then it will do that with
its driver of course.
So therefore you might have a web application where users can locate themselves,
you can do that through some web APIs or you have a mobile app where users can locate themselves. Such
location APIs will always return coordinates in the form of latitude and longitude,
this is the standard format.
So your application will give you some latitude and longitude data for whatever your user did,
for example locate himself.
Now let's fake that part,
so let's simply click somewhere, here for example and now I marked a place where we also see the coordinates.
If I click on that, I can also copy them from there. Again,
the second coordinate is the longitude, first one is the latitude. So this is the location we assume
we're getting from our app,
so how can we now use that location to find out if this place here is near our location and what does
near define?
Well let's see that step by step. In find,
I definitely want to have a look at my location key and if you named that differently, you need to name it
differently here,
this is not a reserved keyword,
this is simply referring to our key here
and then here, the value is a document where I use $near,
$near is one of the operators provided by mongodb for working with
geospatial data.
$near requires another document as a value and there,
you can now define a geometry for which you want to check if it's near,
so $geometry. That again is a document
and this now again describes a geo json object, so we could check here if a point we add here is close
to our point and that's exactly what I'll do.
So since we have to pass in some geo json here, I can add a type and that type will also be a point,
we'll work with different types later by the way and I will pass my coordinates, just as before because
this has to be geo json,
we have to have a type here which is one of the valid types and we have to have coordinates here.
And you therefore of course also know which format coordinates should be in, correct
it should be an array where the first value is the longitude,
second value is the latitude.
So let's paste in that longitude we grab from Google Maps and let's then also grab the latitude
and let's paste this as a second value.
Now if I hit enter, I get an error and the error is telling me that it's unable to find an index for
the geoNear query and geoNear is simply the behind the scenes name of our near query.
So for this, we will need a geospatial index for this query to run.
Not all geospatial queries require index but they all just as with other indexes will most likely
benefit from having such an index,
so let's add a geospatial index in the next lecture.