Database Reference
In-Depth Information
"nscanned" : 3,
"objectsLoaded" : 2,
"avgDistance" : 0.001124255408035117,
"maxDistance" : 0.001126847051610947
},
"ok" : 1
}
The dist field that accompanies each document is a measure of that item's distance
from the center point. In this case, the distance is measured in degrees.
Another slightly more advanced query allows searches within certain boundaries
using the $within query operator. So, for example, to find all ZIP codes within 0.011
degrees of Grand Central Station, you can issue the following $center query:
> center = [-73.977842, 40.752315]
> radius = 0.011
> db.zips.find({loc: {$within: {$center: [ center, radius ] }}}).count()
26
This is theoretically equivalent to running a $near query with the optional $max-
Distance parameter. Both queries return all results with the specified distance from
the center point.
> db.zips.find({'loc': {$near: [-73.977842, 40.752315],
$maxDistance: 0.011}}).count()
26
In addition to the $center operation, you can use the $box operator to return results
lying inside a particular bounding box. So, for example, to return all ZIP codes lying
within the box bounded by Grand Central Station and LaGuardia Airport, you could
issue the following:
> lower_left = [-73.977842, 40.752315]
> upper_right = [-73.923649, 40.762925]
> db.zips.find({loc: {$within:
{$box: [ lower_left, upper_right ] }}}).count()
15
Ta ke n o t e t h a t t h e $box operator requires a two-element array, with the first element
being the lower-left coordinate and the second element being the upper-right coordi-
nate of the desired bounding box.
E.3
Compound spatial indexes
It's possible to create compound spatial indexes, but only if the coordinate key comes
first. You might use a compound spatial index to enable queries on a location in addi-
tion to some type of metadata. For instance, imagine that the gardening store intro-
duced earlier in the topic has retail locations and that different locations provide
different services. A couple of location documents might, in part, look like these:
{loc: [-74.2, 40.3], services: ['nursery', 'rentals']}
{loc: [-75.2, 39.3], services: ['rentals']}
Search WWH ::




Custom Search