2017-08-16 40 views
-2

我想獲得關於我的web項目的推薦。我使用PHP和MongoDB,但當我從php文檔中讀到這句話時,我感到困惑。MongoClient類與MongoDB Driver Manager類

這個定義這個類的擴展已被棄用。相反,應該使用MongoDB擴展。這個類的替代包括: MongoDB \ Driver \ Manager

我已經使用MongoClient類CRUD,但在讀完這句話後,我嘗試將MongoClient遷移到MongoDB \ Driver \ Manager。使用MongoDB的\驅動程序\管理器的連接被successed但我不能再:(

我的PHP版本是29年6月5日。 蒙戈擴展的版本是1.7.0 MongoDB的擴展版本是1.2.9

我的問題是: 我一定要使用MongoDB的\驅動程序\管理類 它比MongoClient類更好

回答

0

下面是有關不推薦使用的語言功能一個很好的答案:?? What does PHP do with deprecated functions?

這裏是PHP中的正確使用與MongoDB的:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 
$filter = []; 
$options = [ 
    'sort' => ['_id' => 1], 
]; 

$query = new MongoDB\Driver\Query($filter, $options); 
$cursor = $manager->executeQuery('db.collection', $query); 

foreach ($cursor as $document) { 
//... 
} 

有被大量使用PHP和MongoDB教程CRUD操作,例如:MongoDB PHP tutorial

總之:你不應該使用過時功能,因爲安全的原因,因爲它可能會在未來從PHP中刪除。所以更好地更新你的代碼。

+0

謝謝!我可以問更多嗎?什麼是MongoDB \ Driver \ Command類?我無法理解它何時被使用。 –

+0

「MongoDB \ Driver \ Command類是**表示數據庫命令**的值對象。」例如:'$ command = new MongoDB \ Driver \ Command(['ping'=> 1]);'或'$ command = new MongoDB \ Driver \ Command([「count」=>「collection」,「query」 => $ query]);' –

+0

使用以下命令可以計算包含此字符串「world」的文檔:'$ query = [「hello」=>「world」]; $ command = new MongoDB \ Driver \ Command([「count」=>「collection」,「query」=> $ query]);'' –

相關問題