地理位置索引

PHP 代码(使用yiisoft/yii2-mongodb):
$longitude = 113.801367;
$latitude = 22.987066;
 
$param = array(
'loc' => array(
'$nearSphere' => array(
'$geometry' => array(
'type' => 'Point',
'coordinates' => array(doubleval($longitude), doubleval($latitude)),
),
'$maxDistance' => 50 * 1000
)
)
);
 
$result = StationMongo::findAll($param);
 
 
var_dump($result);
exit();
 
 
 
 
db.station.insert({"address" : "南京 禄口国际机场","loc" : { "type": "Point", "coordinates": [118.783799,31.979234]}})
db.station.insert({"address" : "南京 浦口公园","loc" : { "type": "Point", "coordinates": [118.639523,32.070078]}})
db.station.insert({"address" : "南京 火车站","loc" : { "type": "Point", "coordinates": [118.803032,32.09248]}})
db.station.insert({"address" : "南京 新街口","loc" : { "type": "Point", "coordinates": [118.790611,32.047616]}})
db.station.insert({"address" : "南京 张府园","loc" : { "type": "Point", "coordinates": [118.790427,32.03722]}})
db.station.insert({"address" : "南京 三山街","loc" : { "type": "Point", "coordinates": [118.788135,32.029064]}})
db.station.insert({"address" : "南京 中华门","loc" : { "type": "Point", "coordinates": [118.781161,32.013023]}})
db.station.insert({"address" : "南京 安德门","loc" : { "type": "Point", "coordinates": [118.768964,31.99646]}})
 
 
db.station.ensureIndex( { loc : "2dsphere" } )
 
db.station.find({loc:{$near: {$geometry: {type: "Point" ,coordinates: [118.783799,31.979234]},$maxDistance: 5000}}}) //5000米以内数据
 
为什么用db.station.find(), 真实collection位于db.qshd.station.
 
该文转载:https://note.youdao.com/share/?id=05f3dd85f8da6823d6a95467a0a01522&type=note#/

转载于:https://www.cnblogs.com/xinhui/p/8920105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值