LBS,儲存每個地點的經緯度座標,搜尋附近的地點,建立地理位置索引可提高查詢效率。
mongodb地理位置索引,2d和2dsphere,對應平面和球面。
1.建立lbs集合存放地點座標
use lbs; db.lbs.insert( { loc:{ type: "Point", coordinates: [113.332264, 23.156206] }, name: "广州东站" } ) db.lbs.insert( { loc:{ type: "Point", coordinates: [113.330611, 23.147234] }, name: "林和西" } ) db.lbs.insert( { loc:{ type: "Point", coordinates: [113.328095, 23.165376] }, name: "天平架" } )
2.建立地理位置索引
db.lbs.ensureIndex( { loc: "2dsphere" } )
3.查詢附近的地理位置索引
db.lbs.find( { loc: { $near:{ $geometry:{ type: "Point", coordinates: [113.323568, 23.146436] }, $maxDistance: 1000 } } } )
3.查詢附近的地理位置索引
{ "_id" : ObjectId("556a651996f1ac2add8928fa"), "loc" : { "type" : "Point", "coordinates" : [ 113.330611, 23.147234 ] }, "name" : "林和西" }
3.查詢附近的地理位置索引<?php // 连接mongodb
function conn($dbhost, $dbname, $dbuser, $dbpasswd){
$server = 'mongodb://'.$dbuser.':'.$dbpasswd.'@'.$dbhost.'/'.$dbname;
try{
$conn = new MongoClient($server);
$db = $conn->selectDB($dbname);
} catch (MongoException $e){
throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31);
}
return $db;
}
// 插入坐标到mongodb
function add($dbconn, $tablename, $longitude, $latitude, $name){
$index = array('loc'=>'2dsphere');
$data = array(
'loc' => array(
'type' => 'Point',
'coordinates' => array(doubleval($longitude), doubleval($latitude))
),
'name' => $name
);
$coll = $dbconn->selectCollection($tablename);
$coll->ensureIndex($index);
$result = $coll->insert($data, array('w' => true));
return (isset($result['ok']) && !empty($result['ok'])) ? true : false;
}
// 搜寻附近的坐标
function query($dbconn, $tablename, $longitude, $latitude, $maxdistance, $limit=10){
$param = array(
'loc' => array(
'$nearSphere' => array(
'$geometry' => array(
'type' => 'Point',
'coordinates' => array(doubleval($longitude), doubleval($latitude)),
),
'$maxDistance' => $maxdistance*1000
)
)
);
$coll = $dbconn->selectCollection($tablename);
$cursor = $coll->find($param);
$cursor = $cursor->limit($limit);
$result = array();
foreach($cursor as $v){
$result[] = $v;
}
return $result;
}
$db = conn('localhost','lbs','root','123456');
// 随机插入100条坐标纪录
for($i=0; $i
3.查詢附近的地理位置索引
use lbs; db.createUser( { "user":"root", "pwd":"123456", "roles":[] } ) db.auth( { "user":"root", "pwd":"123456" } )
3.查詢附近的地理位置索引
rrreee
座標:113.323568, 23.146436
搜尋附近一公里內的點,由近到遠一點 rrreee
示範php程式碼,首先需要在
mongodb的lbs中建立使用者和執行auth
。方法如下:rrreee🎜 🎜 以上就介紹了mongodb 地理位置搜尋,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。 🎜 🎜 🎜
TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用