Database Reference
In-Depth Information
Finally, to find items located within a circular shape on a sphere (say, our planet) you
can use the
$centerSphere
operator. This operator is similar to
$center
, like so:
> db.restaurants.find( { loc: { $geoWithin : { $centerSphere : [ [52.370524,
5.217682], 10] } } } )
By default, the
find()
function is ideal for running queries. However, MongoDB also
provides the
geoNear()
function, which works like the
find()
function, but also displays
the distance from the specified point for each item in the results. The
geoNear()
function
also includes some additional diagnostics. The following example uses the
geoNear()
function to find the two closest results to the specified position:
> db.runCommand( { geoNear : "restaurants", near : { type : "Point",
coordinates: [52.338433, 5.513629] }, spherical : true})
It returns the following results:
{
"ns" : "stores.restaurants",
"results" : [
{
"dis" : 33155.517810497055,
"obj" : {
"_id" : ObjectId("51ace0f380523d89efd199ac"),
"name" : "Kimono",
"loc" : {
"type" : "Point",
"coordinates" : [
52.370451,
5.217497
]
}
}
},
{
"dis" : 69443.96264213261,
"obj" : {
"_id" : ObjectId("51ace13380523d89efd199ae"),
"name" : "Tokyo Cafe",
"loc" : {
"type" : "Point",
"coordinates" : [
52.368736,
4.89053
]
}
}
},