Now to conclude the geospatial queries we can run, let's find all places in a certain radius around
the user.
We kind of did this before with the near command,
now I also want to give you a different way.
The near command also sorted the results,
that is the important difference
and I'll come back to that,
let's now place the user here and let's check for a certain radius and all the points in that radius.
So for that, we'll need the coordinates of the user of course
and I'll write a new query, again against my places collection not the areas collection because the
places collection is the collection with these four places we added, right.
So this is the collection I'm interested in now and there, I want to find all elements in an unsorted
order that are within a certain radius.
So here the location field is the field which stores the coordinates,
so this is the field which I will now use with the geoWithin
operator again. Why geoWithin and not geoIntersects? Because now I want to find all places that are within
a place or an area which I'll define here in the query,
previously we had the opposite,
we had a point in the query and wanted to find an area that surrounds the point.
So geoWithin normally takes a geometry operator which describes a geo json object but there is
a special operator you can use in
mongodb
and that is the centerSphere operator, $centerSphere.
This is a helpful operator that allows you to quickly get a circle around a point,
so essentially it use a radius and a center and gives you the whole circle around that center therefore.
centerSphere takes an array as a value and that array has two elements,
the first element is another array which holds your coordinates, again longitude and then latitude
and with coordinates, I mean the coordinates of the center of the circle you want to draw.
The second element in this array is the radius then
and now this radius needs to be translated manually from meters or miles to radians.
Now this conversion is relatively easy to do and you'll find an article in the mongodb docs
too,
you'll find an article which mentions how to translate miles to radians or kilometers.
Now I will work with kilometers here,
so this is the value which is interesting to me
and now I want to look in a radius of let's say one kilometer,
so one divided by this value without the comma is my radius here.
If I hit enter, I find two points, let me pretty print this, I find the Conservatory of Flowers and the
Golden Gate Tennis Park,
what I don't find is to California Academy of Sciences,
let's quickly measure the distance of this point to this point.
Now it looks like this should be in there,
so my assumption would be that it's simply not in there because I have the wrong coordinates stored.
Let's quickly look into our database for the California Academy of Sciences and indeed, the coordinates
here don't match mine,
that is because previously I didn't have my browser perfectly centered on them.
So let's quickly update that to be really sure that our query here works correctly.
So I'll update by simply searching for that objectId here, places and you know how to update,
I hope you still know it,
first of all you filter for _id being equal to my objectId here
and the second value is then the value where you describe the update with $set.
I will set my location to a new value,
the type will still be a point, that hasn't changed but the coordinates, that will now be a new array
with new values,
first the longitude here and second, the latitude.
So let's grab this and this is now the exact point, the exact location of that institute.
So with this updated, let's run our query again and
now the California Academy of Sciences is included too
but please note that Nopa is not included and that makes sense because Nopa should definitely not
be inside of our circle.
If I measure the distance of this point to this point, it's above two kilometers,
so it makes sense that this is not included.
So this is how we can run such queries as well,
the important difference to the near query which also allowed us to find places within a certain min
and max distance is that with this query,
we get back an unsorted list, this keeps the order of the elements in the database
or we could of course manually sort them with sort method. The near method did give us the elements in
a certain radius and did sort them by proximity
and that really depends on your requirements, what you need, do you need sorted results with the nearest
result first
or do you need unsorted results and you just want to get a list of the elements in an area in general?
This is what geoWithin can do,
near is otherwise your solution for the sorted list.