Mongodb-php
MongoDB-PHP
PHPでMongoDBを使用するには、MongoDB PHPドライバーを使用する必要があります。 Download PHP Driverからドライバーをダウンロードします。 必ず最新リリースをダウンロードしてください。 アーカイブを解凍し、php_mongo.dllをPHP拡張ディレクトリ(デフォルトでは「ext」)に配置し、次の行をphp.iniファイルに追加します-
extension = php_mongo.dll
接続してデータベースを選択する
接続を確立するには、データベース名を指定する必要があります。データベースが存在しない場合は、MongoDBが自動的に作成します。
以下は、データベースに接続するためのコードスニペットです-
<?php
//connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
//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("mycol");
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->mycol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" => "http://www.finddevguides.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->mycol;
echo "Collection selected succsessfully";
$cursor = $collection->find();
//iterate cursor to display title of documents
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
プログラムが実行されると、次の結果が生成されます-
Connection to database successfully
Database mydb selected
Collection selected succsessfully {
"title": "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 Tutorial")));
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"] . "\n";
}
?>
プログラムが実行されると、次の結果が生成されます-
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
"title": "MongoDB Tutorial"
}
ドキュメントを削除する
ドキュメントを削除するには、remove()メソッドを使用する必要があります。
次の例では、 MongoDB Tutorial というタイトルのドキュメントを削除します。 以下は、ドキュメントを削除するためのコードスニペットです-
<?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 remove the document
$collection->remove(array("title"=>"MongoDB Tutorial"),false);
echo "Documents deleted successfully";
//now display the available documents
$cursor = $collection->find();
//iterate cursor to display title of documents
echo "Updated document";
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
プログラムが実行されると、次の結果が生成されます-
Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully
上記の例では、2番目のパラメーターはブール型で、* remove()メソッドの *justOne フィールドに使用されます。
残りのMongoDBメソッド* findOne()、save()、limit()、skip()、sort()*など 上で説明したのと同じ働きをします。