PHP連接MongoDB操作
要使用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" => "http://www.yiibai.com/mongodb/",
"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=> 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
在上面的例子中,第二個參數是布爾類型,用於remove()
方法的justOne
字段。
其餘的MongoDB方法findOne()
,save()
,limit()
,skip()
,sort()
等與上述相同。
由於此篇教程文章是一個入門級的教程文章,只是講解有關簡單入門的操作,有關更高級的內容,請參考:http://docs.mongodb.com/php-library/