Yii Emongodocuments near queries ignoring maxDista

2019-09-11 12:21发布

I am trying to use $near to find places that are near my users.

    $places = Places::model ()->findAll(array (
            "conditions" => array (
                'location' => Array('near' => array((float)$this->latitudeUser,(float)$this->longitudeUser)),
            ),
            "maxDistance" => 1,
            "limit" => 5,
        ));

Everything seems to work except that it finds places as far as Texas USA and I am in Montreal Canada. I have no idea what I can do to make maxDistance work. It seems I cannot use GeoNear or nearSphere since EMongoDocuments does not support them it seems.

So am I missing something obvious?

I am aware of mongomapper-near-with-maxdistance-mongooperationfailure-geo-values-have-to but nothing there helped.

edit: changed order of longitude lattitude

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-11 13:01

EMongoCriteria in YiiMongoDbSuite changes the mongo operators' char case, maxDistance will turn into "$maxdistance", then the mongodb server will ignore it and return all 100 items near the user's location. To use MongoYii, it will not change your query conditions: http://sammaye.github.io/MongoYii/

查看更多
男人必须洒脱
3楼-- · 2019-09-11 13:09

Quoting the fantastic guy's at mongolabs: I believe you need to put maxDistance inside the $near conditional. Something like this:

$places = Places::model ()->findAll(array (
"conditions" => array (
'location' => Array('near' => array((float)$this->latitudeUser,(float)$this->longitudeUser), 'maxDistance' => 1),
),
"limit" => 5,
));

I'm not familiar with this framework so I'm not completely sure if that's right, but I'm basing this on the proper syntax for the equivalent MongoDB query:

db.places.find( { location : { $near : [50,50] , $maxDistance : 5 } } ).limit(5)

Notice $maxDistance is inside the same condition as $near. Give that a try and let us know if it helps. Thanks,

查看更多
女痞
4楼-- · 2019-09-11 13:22

MongoDB wants coordinates in the format : latitude, longitude. Your array has it the wrong way around. The distance should be in degrees, unless you use nearSphere. The results from nearSphere should be similar than to near, so EMongoDocuments should support. In case you use nearSphere, the maxdistance is in radians, so divide the distance (in km) by the radius of the Earth (in km, ~6371), if you use that.

查看更多
登录 后发表回答