I'm using PHP and MongoDB to sore and to get the nearest points to my position. But I have a problem.
When I use the mongo console, it returns values but when I'm doing by PHP code, it returns a null object.
PHP Code:
$connection = new MongoClient( "mongodb://localhost" );
$db = $connection->collection;
$collection = $db->user_location;
// There are many differnt queries (by token, by email...)
if (isset($_GET['email'])) {
$query = array(
//'email' => array( '$neq' => $_GET['email']),
'loc' => array(
'$near' => array(
'lon' => floatval($_GET['longitude']),
'lat' => floatval($_GET['latitude']),
'$maxDistance' => intval($_GET['distance']))));
}
$maxDistance = $_GET["distance"];
$lon = $_GET['longitude'];
$lat = $_GET['latitude'];
$cursor = $collection->find($query);
if ($cursor) {
echo json_encode($cursor);
} else {
echo "{ 'status' : 'false' }";
}
This is an example of mongo console use: db.user_location.ensureIndex({ loc : "2d"} )
db.user_location.find() { "_id" : ObjectId("51dbe817617c6df061000000"), "email" : "zgz", "loc" : { "lon" : -0.88588498, "lat" : 41.68035677 }, "date" : "Tue, 09 Jul 2013 06:38:15" } db.user_location.find({"loc": {"$near": {"lon":-0.88588498,"lat":41.68035677}, "$maxDistance":99}}) { "_id" : ObjectId("51dbe817617c6df061000000"), "email" : "zgz", "loc" : { "lon" : -0.88588498, "lat" : 41.68035677 }, "date" : "Tue, 09 Jul 2013 06:38:15" }
If I use the same values in my PHP code, it returns a null cursor. Why? Thanks!