2017-04-07 260 views
0

我使用的是Windows 10 64位,xampp 3.2.2,PHP 5.6.30,PHP Extension Build VC11,MongoDB服務器版本:3.4.3。如何解決致命錯誤:類'MongoClient'找不到?

我越來越喜歡 「致命錯誤:類 'MongoClient' 在d未發現:4號線\ XAMPP \ htdocs中\測試\ test1.php」 的錯誤。

這裏是我使用

CODE

<?php 

// connect 
$m = new MongoClient(); 

// select a database 
$db = $m->cabin; 

// select a collection (analogous to a relational database's table) 
$collection = $db->user; 

// find everything in the collection 
$cursor = $collection->find(); 

// iterate through the results 
foreach ($cursor as $document) { 
    echo $document["title"] . "\n"; 
} 

?> 

我已經添加在php.ini (D:\xampp\php) 「延長= php_mongodb.dll」 文件夾中(D:\xampp\php\ext) dll文件,並添加擴展的代碼。但問題沒有解決。

的MongoDB(蒙戈分貝正在工作)

[email protected] MINGW64 /d 
$ mongo 
MongoDB shell version v3.4.3 
connecting to: mongodb://127.0.0.1:27017 
MongoDB server version: 3.4.3 
use cabin 
switched to db cabin 
show tables 
bmessages 
booking 
cabins 
club 
country 
customer 
email 
facilities 
land 
mschool 
region 
role 
rooms 
settings 
tempuser 
tour 
user 
userold 
visit 

回答

0

MongoClient屬於長棄用Mongo extension。新的MongoDB extension使用Manager連接到數據庫,例如,

$m = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 
$cursor = $manager->executeQuery("cabin.user", new MongoDB\Driver\Query([])); 
foreach ($cursor as $document) { 
    echo $document["title"] . "\n"; 
} 

或者使用任何更高級別的抽象庫。例如。 https://github.com/mongodb/mongo-php-library提供類似於傳統驅動程序的界面。

0

MongoDB \ Driver \ Manager負責維護與MongoDB的連接。

連接

$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 

列表數據庫

$listdatabases = new MongoDB\Driver\Command(["listDatabases" => 1]); 
$res = $mng->executeCommand("admin", $listdatabases); 

讀取所有數據

$query = new MongoDB\Driver\Query([]); 

$rows = $mng->executeQuery("database_name.collection_name", $query); 

foreach ($rows as $row) { 

    echo "$row->name\n"; 
} 

批量寫入(同時執行兩個或多個操作)

$bulk = new MongoDB\Driver\BulkWrite; 

$doc = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'Toyota', 'price' => 26700]; 
$bulk->insert($doc); 
$bulk->update(['name' => 'Audi'], ['$set' => ['price' => 52000]]); 
$bulk->delete(['name' => 'Hummer']); 

$mng->executeBulkWrite('testdb.cars', $bulk);