要使用PHP與MongoDB互動儲存資料,需要使用MongoDB PHP驅動程式(http://pecl.php.net/package/mongo)。 從url下載驅動程式下載PHP驅動程式並確保下載的是正確的版本(如在本範例中:Win10 64位元下載的版本是:php_mongo-1.6.8-5.6-ts-vc11-x64.zip
)。 現在解壓縮存檔並將php_mongo.dll
放入PHP擴充套件目錄(預設為「ext
」),並將以下行新增到php.ini
檔案中 -
extension = php_mongo.dll
然後重新啟動 Apache 伺服器,檢視 phpinfo()
函式的輸出結果 -
要進行連線,需要指定資料庫名稱,如果資料庫不存在,那麼MongoDB會自動建立它。
以下是連線到資料庫的程式碼片段 -
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully<br/>";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
?>
執行程式時,會產生以下結果 -
Connection to database successfully
Database mydb selected
以下是建立集合的程式碼片段 -
<?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";
?>
執行程式時,會產生以下結果 -
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" => "/18/141/4072.html",
"by", "tutorials point"
);
$collection->insert($document);
echo "Document inserted successfully";
?>
執行程式時,會產生以下結果 -
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=> /18/141/4072.html
要更新文件,需要使用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=> /18/141/4072.html
要刪除文件,可使用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
在上面的例子中,第二個引數是布林型別,用於remove()
方法的justOne
欄位。
其餘的MongoDB方法findOne()
,save()
,limit()
,skip()
,sort()
等與上述相同。
由於此篇教學文章是一個入門級的教學文章,只是講解有關簡單入門的操作,有關更高階的內容,請參考:http://docs.mongodb.com/php-library/