MongoDB Geospatial queries in php

I am trying to use MongDBs 2d indexing to run geospatial queries as described here - http://www.mongodb.org/display/DOCS/Geospatial+Indexing

It works fine, and I can run queries like

db.places.find( { point : { $near : [151.1955562233925,-33.87107475181752]  , $maxDistance : 0.1/111} } )

from the CLI tool, but when I try to run the request in PHP (using the PGL mongo driver), I cannot get any results.

Any help in writing the above query for PHP? Does anyone know if the PHP driver supports geospatial queries?

thank

+5
source share
5 answers

I have a geo search with a PHP driver working in production. It looks something like this:

$latLong = $geo->lookupLatLong($address);
$cursor = $coats->find(Array('latLong' => Array('$near' => $latLong)))->limit(10);

, $nearSphere, $, . . mongo docs.

+7

PHP mongo.

$collection->find(Array("point" => Array('$within' => Array('$center'=> Array(Array(151.1955562233925,-33.87107475181752), 0.1/111 ) ) )));

, , lat-longs . mongo (, ), ( ).

. Cakephp mongo -. https://github.com/ichikaway/cakephp-mongodb/blob/master/samples/controllers/geos_controller.php

+1

, "" PHP.

geoNear MongoDb:

$db->command(array('geoNear'=>'places','near'=>array(151.1955562233925,-33.87107475181752),'num'=>9));
0

$nearSphere PHP :

$lonlat = array($lon, $lat);
$cursor = $this->collection->find(Array('loc' => Array('$nearSphere' => $lonlat)))->limit($limit);

Where "loc" is the 2nd geospatial index, and the limit is optional.

0
source

According to documentation in MongoDB website

{ $near: { $geometry: { type: "Point" , coordinates: [ <longitude> , <latitude> ] }, $maxDistance: <distance in meters>, $minDistance: <distance in meters> } }

Php code for above request:

$query = array(
                'loc' => array(
                    '$near' => array(
                        '$geometry' =>array(
                            'type' => 'Point',
                            'coordinates'=>array(floatval($lon),floatval($lat)),
                            '$maxDistance' => intval($max_distance),
                            '$mixDistance' => intval($min_distance)))));
0
source

All Articles