Database Reference
In-Depth Information
{
"_id" : ObjectId("51ace13380523d89efd199ae"),
"name" : "Tokyo Cafe",
"loc" : {
"type" : "Point",
"coordinates" : [ 52.368736, 4.89053 ]
}
}
{
"_id" : ObjectId("51ace11b80523d89efd199ad"),
"name" : "Shabu Shabu",
"loc" : {
"type" : "Point",
"coordinates" : [ 51.915288, 4.472786 ]
}
}
Although this set of results certainly looks better, there's still one problem: all of the
documents are returned! When used without any additional operators, $near returns the
first 100 entries and sorts them based on their distance from the given coordinates. Now,
while we can choose to limit our results to say, the first two items (or two hundred, if we
want) using the limit function, even better would be to limit the results to those within
a given range.
This can be achieved by appending the $maxDistance operator. Using this operator
you can tell MongoDB to return only those results falling within a maximum distance
(measured in meters) from the given point, as in the following example and its output:
> db.retaurants.find( { loc : { $geoNear : { $geometry : { type : "Point",
coordinates: [52.338433, 5.513629] }, $maxDistance : 40000 } } } )
{
"_id" : ObjectId("51ace0f380523d89efd199ac"),
"name" : "Kimono",
"loc" : {
"type" : "Point",
"coordinates" : [ 52.370451, 5.217497 ]
}
}
As you can see, this returns only a single result: a restaurant located within 40 kilometers
(or, roughly 25 miles) from the starting point.
there is a direct correlation between the number of results returned and the time
a given query takes to execute.
Note
 
 
Search WWH ::




Custom Search