I'm back in the terminal here and I will enter a new database, awesome places by using it
and in this database, I want to add some nice places, essentially locations which I can then query and
we'll do this together right from start,
we'll also add these places right from start so that you can understand how geospatial data needs
to be stored so that you can work with it. In the official docs,
you'll find an article about geo json, how it's structured and which kind of geo json objects mongodb
supports because geo json is not a standard invented by mongodb,
it's actually available outside of mongodb too
and there, you have more objects available that are supported by mongodb
but mongodb supports
all major important objects like points, lines or polygons, as well as some special advanced objects.
Now this article is definitely worth checking out
but the most important thing here is that you understand how a geo json object is created and creating
it is very simple. To work with some data,
I chose the San Francisco area
but of course you can open up Google maps and work with different locations.
I'll use Google Maps to pick some locations so that we also can see something on the map and kind of
validate whether our queries work correctly.
Now let's work with that Golden Gate Park and let's start by adding one simple location in our database,
let's say we are going to pick some awesome places.
I like this park quite a lot actually,
I've been there, I've not been to every place I'm going to pick now but let's simply pick some places
here, like the California Academy of Sciences. Now in Google Maps if you click on a place, you can easily
access the coordinates of that place from inside the URL, you'll find them here.
Now the important thing is the first coordinate until the first comma is the latitude,
the second one is the longitude and you'll need to remember that to store it correctly in mongodb.
Coordinates are always expressed as latitude and longitude
or longitude and latitude pairs because the longitude essentially describes a position on a
vertical axis if you draw it onto our globe, onto the earth globe and the latitude describes a horizontal
axis on the earth globe
and with that coordinate system, we can map any point onto our earth.
So let's work with that data and let's add this as an awesome place to our collection.
So here I'll add a new places collection and I'll insert one new place. Now you insert a normal document,
this doesn't change and I'll give it a name of California Academy of Sciences because that is the name
that we see here
and then I want to add the coordinates in geo json format. Now
there's nothing special about geo json,
you can add any key you want with any name you want,
I'll name it location but you could name it loc, you could name it totally different,
that does not matter,
what matters is the structure of this value.
The value should be an embedded document and in that embedded document, you'll need two pieces of information
and these two pieces of information are also mentioned in the official docs,
you'll need to define the type and you'll need to define the coordinates.
The coordinates are then an array where the first value
and now this is really important, where the first value has to be the longitude
and the second value is the latitude,
so let's add a point exactly in the way we can see it here.
Let's go back and grab our longitude first,
so this second value here on Google Maps,
copy that
and then here, I will first of all add a type and type here is point because I add a single point and
then I add my coordinates and now these key names here are not up to you,
this one has to be named type and it has to be one of the supported types like point and this one has
to be named coordinates and it has to be an array where the first value is now our longitude and the
second value separated with a comma is then our latitude,
so let's grab that latitude here and enter this here.
With this I added this place
and therefore, if I now simply find one, the only place I added thus far, this is now my geo json object,
exactly this
and it is treated as geo json because it fulfills the requirement of having a type which is one
of the supported objects and of having coordinates which is an array where the first value is treated
as the longitude and the second value is treated as a latitude.
Now with that point added, we know how geo json looks like,
let's now find out how we can query that geo json data.