Okay, so now I'd like to talk about another type
of index. Geospatial indexes, in particular. Support for geospatial indexes in
MongoDB give us the ability to perform queries for locations
near another location. So, the type of thing that a lot
of phone apps will do, when you're say, looking for
a nearby cafe. Now the type of geospatial index that we're
going to talk about is 2D geospatial indexes. But I'm going
to be, also provide support for spherical geospatial indexes, that is
those that take into account the curvature of the earth.
But I'll direct you to the documentation and our courses
at MongoDB University if you're interested in learning more about
those. So, with 2D Geospatial Indexes, we're essentially thinking of our
data as all lying on a Cartesian plane, with values
in the x direction and the y direction. So, in these
situations we have a query location of some kind, and
what we want to find in response to queries are other
items or documents that are close to this query location.
So there's essentially three things that you need to know about
in order to construct a geospacial index in MongoDB. The
first of those is that you need a field that holds
a location, and the location needs to be stored as
an array with first an x value and then a y
value. Now you can name this field whatever you want. I've
just chosen the name location here. It could be loc or
position point anything you like, but it does need
to follow this form of having the x value first
and then the y. The second thing you need to
know about is that you need to ensure there's an
index, need to all ensure index and create an
index on this particular field. So in this case I
would need to call insure index. Specifying location as the
field, and I would need to specify a direction here.
We'll take a look at a specific example of that
in just a bit. So again, we need to create an
index now on this field that we have for our
documents that we want to use in geospatial queries. And finally
the way we do queries against the geospatial index, is
through the use of the $near operator. So it's these three
steps in combination that allow us to do something like this,
and get all of the documents that have a location near
this one. So let's take a look at this in some
code, and then we'll do an example query in the Mongo
shell. So this is a script that I actually retrieved from
the OpenStreetMap, folks. This is a script that they wrote
for putting OSM data into MongoDB. So you could see here
that it's going to do iterative parsing of our Osm data,
just like we did in a previous example back in lesson
three. Now, let's take a look a little bit further down first,
because I want to show you the location
field here. So, for every node in this file,
this script builds a value called loc and it
is composed of the latitude value and the longitude
value for a node element in the XML file.
Then it adds that field to the record that
it's building up as it moves through the node
that it's creating a document for, okay? And then,
finally, it will end up creating a document
in MongoDB by doing an insert at some point.
Okay? Now, using that location field, at the
very top of this code. Ensure_index, it's called. And
ensure_index is called using that location field which
stores the xy coordinates. Four nodes that are parsed
out of the OSM XML file. Now, the syntax
for ensure_index is slightly different in pymongo. It matches
the syntax for the language here, which of course is
Python. Okay? But then also, rather than passing a dictionary,
what we pass instead is a list of tuples. So,
we pass the name of the field that we want
to create an index on, as well as a direction.
So these are constant values that are available to us
on pymongo. So we're not simply typing strings here, which
is potentially very airprone. Okay? And you can see that,
since your index is actually being used here to
create several indexes, here's another example where we're creating an
index on id and we're specifying that we want
that index to be created in ascending order. Okay? So,
technically speaking, GEO2D is a direction argument for ensure_index.
And the reason why it makes sense to think about
this as a direction argument, is because queries using
the near operator will always return documents sorted by those
that are nearest to the query location. Okay, so now
let's take a look at an actual query, and bear in
mind that the query we're going to look at in the Mongo
shell is a query against the collection that we created using
this script. This is exactly the script that I used to
create this collection and store documents in it within MongoDB. Okay
so let's take a look at query here. Now what I
need to do is make sure that I'm using the OSM
data base and then I'm going to create
the nodes collection that script we just looked
at actually creates several collections one of which
is nodes and these are based on the nodes
tags that appear in the OSM dataset. Now
again, we're using the Chicago OSM dataset just for
point of clarification. Okay, so what I'm going
to do here then is query this particular collection,
and note that I'm querying against location field. And
I'm using the near operator, okay? And I'm specifying a
set of coordinates here. Okay? So, this is the
type of thing that an application might do in order
to find restaurants or other amenities near the location
of a user making the query. Okay, this is how
we might do this sort of thing in the
back-end of an application that supports that type of functionality.
Now, I'm doing one other thing here, and this is just is
really purposes of this example. And you remember from a little earlier in
the lesson when we looked at the tags that get applied to nodes.
That there is this tg field in this collection. Okay? And just so
that the data we get back is a little bit more interesting,
I'm just ensuring that there is a tg field, that it actually exists.
Because then we'll have some data that has some names and other sort
of tagging associated with it. So, we can kind of figure out what's
there, near this particular location. Now, this location happens
to be quite close to Wrigley Field. So, what we're
going to get are a number of restaurants, cafes, convenient
stores, that sort of thing in that neighborhood, so imagine
you've just walked out of Wrigley Field and you're looking
to see what's nearby on your phone. This is a
type of query we might do in the backend of
application to support this sort of thing. Okay. So here's
our initial set of results. We could get more by typing IT here in the shell.
Right? We can see there's a Jamba Juice.
There's a school, church, convenience store in this
case, happens to be a Walgreens, Domino's Pizza,
and a Dunkin' Donuts. Okay, so that's pretty
much what you need to know in order
to build geospatial indexes In MongoDB. We'll take a
look at using geospacial indexes in the case study in the next lesson.