开发者

Different results from PHP's driver and MongoDB console when using $near

I'm getting different results开发者_Python百科 depending on where I ask from. I have a Mongo collection "stops" indexed by 2d co-ordinates.

From the Mongo Console:

> 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 ] }

My PHP code:

$mdb=new Mongo();
$result=$mdb->myapp->stops->find(array('loc'=>array('$near'=>array(floatval($this->userLongitude),floatval($this->userLatitude)))))->limit(3);

I'm new to Mongo, but I'm pretty sure that code is right. It returns values... just, the wrong ones. Here's what var_dump(iterator_to_array($result)); outputs:

array
    '4e8e0e67597535e4cb002092' => 
        array
        'stop_id' => float 373055431
        'stop_name' => string 'STOP A' (length=34)
        'loc' => 
        array
            0 => float -##.937874
            1 => float ##.442722
    '4e8e0e64597535e4cb00022d' => 
        array
        'stop_id' => float 4245689695
        'stop_name' => string 'STOP B' (length=32)
        'loc' => 
        array
            0 => float -##.937515
            1 => float ##.442692
    '4e8e0e67597535e4cb0020af' => 
        array
        'stop_id' => float 134217173
        'stop_name' => string 'STOP C' (length=21)
        'loc' => 
        array
            0 => float -##.938965
            1 => float ##.443787

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?

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.)

EDIT:Solved 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.


The problem existed in my PHP code. My own fault. See the edit in the original question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜