Database Reference
In-Depth Information
the ensureIndex() function is used to add a custom index. Don't worry about
the syntax of this function yet—you will learn how to use ensureIndex() in depth in the
next chapter.
Note
The 2dsphere parameter tells ensureIndex() that it's indexing a coordinate or
some other form of two-dimensional information on an Earth-like sphere. By default,
ensureindex() assumes that a latitude/longitude key is given, and it uses a range of -180
to 180 . However, you can overwrite these values using the min and max parameters:
> db.restaurants.ensureIndex( { loc: "2dsphere" }, { min : -500 , max : 500 } )
You can also expand your geospatial indexes by using secondary key values
(also known as compound keys ). This structure can be useful when you intend to query
on multiple values, such as a location (geospatial information) and a category
(sort ascending):
> db.restaurants.ensureIndex( { loc: "2dsphere", category: 1 } )
at this time, the geospatial implementation is based on the idea that the world is
a perfect sphere. thus, each degree of latitude and longitude is exactly 111km (69 miles) in
length. however, this is only true exactly at the equator; the further you move away from the
equator, the smaller each degree of longitude becomes, approaching zero at the poles.
Note
Querying Geospatial Information
In this chapter, we are concerned primarily with two things: how to model the data and how
a database works in the background of an application. That said, manipulating geospatial
information is increasingly important in a wide variety of applications, so we'll take a few
moments to explain how to leverage geospatial information in a MongoDB database.
Before getting started, a mild word of caution. If you are completely new to
MongoDB and haven't had the opportunity to work with (geospatial) indexed data in the
past, this section may seem a little overwhelming at first. Not to worry, however; you can
safely skip it for now and come back to it later if you wish to. The examples given serve
to show you a practical example of how (and why) to use geospatial indexing, making it
easier to comprehend. With that out of the way, and if you are feeling brave, read on.
Once you've added data to your collection, and once the index has been created,
you can do a geospatial query. For example, let's look at a few lines of simple yet powerful
code that demonstrate how to use geospatial indexing.
 
 
Search WWH ::




Custom Search