我会根据我的要求取得不同的结果。 我有一个由2名协调人员组成的“站”收集。
摘自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 ] }
我的购买力平价代码:
$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
一切都采用[长期、灵活]的形式,如Mongo docs所言。 我不知道造成不同结果的原因,但也许我会把PHP的盘点推到底,而是收集了返回数据。 什么错误?
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.