Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferent results from PHP's driver and MongoDB console when using $near
    primarykey
    data
    text
    <p>I'm getting different results depending on where I ask from. I have a Mongo collection "stops" indexed by 2d co-ordinates.</p> <p>From the Mongo Console:</p> <pre><code>&gt; db.stops.find({loc:{$near: [-##.94248402000001,##.444653126]}}).limit(3) { "_id" : ObjectId("4e8e0e64597535e4cb0001bd"), "stop_id" : 873198944, "stop_name" : "STOP ONE", "loc" : [ -##.942535, ##.444538 ] } { "_id" : ObjectId("4e8e0e66597535e4cb001260"), "stop_id" : 1603800857, "stop_name" : "STOP TWO", "loc" : [ -##.94355, ##.444672 ] } { "_id" : ObjectId("4e8e0e66597535e4cb000bb7"), "stop_id" : 3659710794, "stop_name" : "STOP THREE", "loc" : [ -##.94355, ##.444672 ] } </code></pre> <p>My PHP code:</p> <pre><code>$mdb=new Mongo(); $result=$mdb-&gt;myapp-&gt;stops-&gt;find(array('loc'=&gt;array('$near'=&gt;array(floatval($this-&gt;userLongitude),floatval($this-&gt;userLatitude)))))-&gt;limit(3); </code></pre> <p>I'm new to Mongo, but I'm pretty sure that code is right. It returns values... just, the wrong ones. Here's what <code>var_dump(iterator_to_array($result));</code> outputs:</p> <pre><code>array '4e8e0e67597535e4cb002092' =&gt; array 'stop_id' =&gt; float 373055431 'stop_name' =&gt; string 'STOP A' (length=34) 'loc' =&gt; array 0 =&gt; float -##.937874 1 =&gt; float ##.442722 '4e8e0e64597535e4cb00022d' =&gt; array 'stop_id' =&gt; float 4245689695 'stop_name' =&gt; string 'STOP B' (length=32) 'loc' =&gt; array 0 =&gt; float -##.937515 1 =&gt; float ##.442692 '4e8e0e67597535e4cb0020af' =&gt; array 'stop_id' =&gt; float 134217173 'stop_name' =&gt; string 'STOP C' (length=21) 'loc' =&gt; array 0 =&gt; float -##.938965 1 =&gt; float ##.443787 </code></pre> <p>Everything is in [longitude,latitude] format, like the Mongo docs say to do. I don't know what is causing the different results, except for maybe I've botch the PHP query, but it's returning data. What's wrong?</p> <p><em>I've stripped out the IDs for brevity and changed some of the data that shouldn't matter except for privacy (namely, the ## hide the degrees and the stop names are different— if it's critical to see those, I can edit it, but everything else is untouched.)</em></p> <p><strong>EDIT:Solved</strong> My code to filter/parse longitude and latitude was also truncating the co-ordinates to only two decimals places. When I used the console directly, there wasn't any truncation going on.</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.
    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