I have a collection in MongoDB called restaurants
{
"_id" : ObjectId("5281f8d660ad39040c000001"),
"name" : "Bucksters Coffee",
"serves" : "Fast Food",
"location" : [
"23.755339",
"90.375408"
]
}
{
"_id" : ObjectId("5285cf0860ad39380b000000"),
"name" : "A1 Donuts",
"serves" : "Fast Food",
"location" : [
"18.5087016",
"73.8124984"
]
}
{
"_id" : ObjectId("5285cf3f60ad39380b000002"),
"name" : "B1 Donuts",
"serves" : "Fast Food",
"location" : [
"18.4893148",
"73.8213213"
]
}
{
"_id" : ObjectId("5285e7a260ad39380b000009"),
"name" : "C1 Donuts",
"serves" : "Fast Food",
"location" : [
"18.5308225",
"73.8474647"
]
}
And my location is ["18.5170345","73.83476"] and I want to find nearest restaurants from my location around 5 kms.
so I try following things,
for insert restaurants,
<?php
try {
$conn = new Mongo('localhost');
// access database
$db = $conn->demo;
// access collection
$collection = $db->restaurants;
$forlatadd="shivaji nagar, pune";
//for lat& Long
$prepAddr = str_replace(' ','+',$forlatadd);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$a=array("$lat","$long");
//print_r($a);
$document = array( "name" => "C1 Donuts","serves" => "Fast Food","location" =>$a);
$collection->insert($document);
$collection->ensureIndex(array("location" => "2d"));
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
And for searching data,
<?php
try {
$conn = new Mongo('localhost');
// access database
$db = $conn->demo;
// access collection
$collection = $db->restaurants;
$forlatadd="deccan, pune";
//for lat& Long
$prepAddr = str_replace(' ','+',$forlatadd);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$a=array("$lat","$long");
//print_r($a);
$distance='5000';
$query = array('location' => array('$near' => array($lat,$long),'$maxDistance' => intval($distance)));
$cursor = $collection->find($query);
if ($cursor) {
echo json_encode(iterator_to_array($cursor));
} else {
echo "{ 'status' : 'false' }";
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
But this gives following error
Error: localhost:27017: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { location: { $near: [ 18.5170345, 73.83476 ], $maxDistance: 5000 } }
But i already done
$collection->ensureIndex(array("location" => "2d"));
while inserting new restaurants in collection, Please help me to find nearest restaurants from my locations.
It working for me,
Only change,
$a=array("$lat","$long"); //It takes lat & long as string
to
$a=array($long,$lat); //It takes lat&long as float.
And its working
In mongodb docs, they specify order in a location definition and indeed I had problems with the order in my previous dev :
Important Specify coordinates in this order: “longitude, latitude.”
http://docs.mongodb.org/manual/reference/operator/query/near/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.