要使用PHP與MongoDB交互存儲數(shù)據(jù),需要使用MongoDB PHP驅動程序(http://pecl.php.net/package/mongo)。 從url下載驅動程序下載PHP驅動程序并確保下載的是正確的版本(如在本示例中:Win10 64位下載的版本是:php_mongo-1.6.8-5.6-ts-vc11-x64.zip)。 現(xiàn)在解壓縮存檔并將php_mongo.dll放入PHP擴展目錄(默認為“ext”),并將以下行添加到php.ini文件中 -
extension = php_mongo.dll
然后重新啟動 Apache 服務器,查看 phpinfo() 函數(shù)的輸出結果 -

要進行連接,需要指定數(shù)據(jù)庫名稱,如果數(shù)據(jù)庫不存在,那么MongoDB會自動創(chuàng)建它。
以下是連接到數(shù)據(jù)庫的代碼片段 -
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully<br/>";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
?>
執(zhí)行程序時,會產(chǎn)生以下結果 -
Connection to database successfully
Database mydb selected
以下是創(chuàng)建集合的代碼片段 -
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->createCollection("phpcol");
echo "Collection created succsessfully";
?>
執(zhí)行程序時,會產(chǎn)生以下結果 -
Connection to database successfully
Database mydb selected
Collection created succsessfully
要將文檔插入到MongoDB中,請使用insert()方法。
以下是插入文檔的代碼片段 -
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->phpcol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" => "http://www.yiibai.com/mongodb/",
"by", "tutorials point"
);
$collection->insert($document);
echo "Document inserted successfully";
?>
執(zhí)行程序時,會產(chǎn)生以下結果 -
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully
要從集合中選擇所有文檔,請使用find()方法。
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->phpcol;
echo "Collection selected succsessfully";
$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document) {
echo $document["title"]. ', URL is=> ' .$document["url"] . "
";
}
?>
以下是選擇所有文檔的代碼片段 -
Connection to database successfully
Database mydb selected
Collection selected succsessfully
MongoDB, URL is=> http://www.yiibai.com/mongodb/
要更新文檔,需要使用update()方法。
在下面的例子中,將把插入的文檔的標題更新為:MongoDB教程 。 以下是更新文檔的代碼段 -
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
// now update the document
$collection->update(array("title"=>"MongoDB"),
array('$set'=>array("title"=>"MongoDB教程")));
echo "Document updated successfully";
// now display the updated document
$cursor = $collection->find();
// iterate cursor to display title of documents
echo "Updated document";
foreach ($cursor as $document) {
echo $document["title"] .', URL => '.$document["url"] . "
";
}
?>
以下是選擇所有文檔的代碼片段 -
Connection to database successfully
Database mydb selected
Collection selected succsessfully
MongoDB, URL is=> http://www.yiibai.com/mongodb/
要刪除文檔,可使用remove()方法。
在下面的示例中,將刪除標題為:MongoDB教程 的文檔。 以下是刪除文檔的代碼片段 -
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->phpcol;
echo "Collection selected succsessfully";
// now remove the document
$collection->remove(array("title"=>"MongoDB教程"),false);
echo "Documents deleted successfully";
// now display the available documents
$cursor = $collection->find();
foreach ($cursor as $document) {
echo $document["title"] . "
";
}
?>
以下是選擇所有文檔的代碼片段 -
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Documents deleted successfully
在上面的例子中,第二個參數(shù)是布爾類型,用于remove()方法的justOne字段。
其余的MongoDB方法findOne(),save(),limit(),skip(),sort()等與上述相同。
由于此篇教程文章是一個入門級的教程文章,只是講解有關簡單入門的操作,有關更高級的內容,請參考:http://docs.mongodb.com/php-library/