Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn null object with PHP using MongoDB $near query
    primarykey
    data
    text
    <p>I'm using PHP and MongoDB to sore and to get the nearest points to my position. But I have a problem.</p> <p>When I use the mongo console, it returns values but when I'm doing by PHP code, it returns a null object.</p> <p>PHP Code:</p> <pre><code>$connection = new MongoClient( "mongodb://localhost" ); $db = $connection-&gt;collection; $collection = $db-&gt;user_location; // There are many differnt queries (by token, by email...) if (isset($_GET['email'])) { $query = array( //'email' =&gt; array( '$neq' =&gt; $_GET['email']), 'loc' =&gt; array( '$near' =&gt; array( 'lon' =&gt; floatval($_GET['longitude']), 'lat' =&gt; floatval($_GET['latitude']), '$maxDistance' =&gt; intval($_GET['distance'])))); } $maxDistance = $_GET["distance"]; $lon = $_GET['longitude']; $lat = $_GET['latitude']; $cursor = $collection-&gt;find($query); if ($cursor) { echo json_encode($cursor); } else { echo "{ 'status' : 'false' }"; } </code></pre> <p>This is an example of mongo console use: db.user_location.ensureIndex({ loc : "2d"} )</p> <blockquote> <p>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" }</p> </blockquote> <p>If I use the same values in my PHP code, it returns a null cursor. Why? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload