May 17, 2021 MongoDB
To use mongodb in php you must use the php driver of mongodb.
MongoDB PHP installation and drive package download on each platform See: PHP installs MongoDB extended driver
To ensure a proper connection, you need to specify the database name, which is automatically created if the database does not exist in the mongoDB
The code snippet is as follows:
<?php // 连接到mongodb $m = new MongoClient(); echo "Connection to database successfully"; // 选择一个数据库 $db = $m->mydb; echo "Database mydb selected"; ?>
To execute the above procedure, the output is as follows:
Connection to database successfully Database mydb selected
The code snippets that create the collection are as follows:
<?php // 连接到mongodb $m = new MongoClient(); echo "Connection to database successfully"; // 选择一个数据库 $db = $m->mydb; echo "Database mydb selected"; $collection = $db->createCollection("mycol"); echo "Collection created succsessfully"; ?>
To execute the above procedure, the output is as follows:
Connection to database successfully Database mydb selected Collection created succsessfully
Insert a document using the insert() method in the mongoDB:
Insert the document code snippet as follows:
<?php // 连接到mongodb $m = new MongoClient(); echo "Connection to database successfully"; // 选择一个数据库 $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "//www.w3cschool.cn/mongodb/", "by", "w3cschool.cn" ); $collection->insert($document); echo "Document inserted successfully"; ?>
To execute the above procedure, the output is as follows:
Connection to database successfully Database mydb selected Collection selected succsessfully Document inserted successfully
Use the find() method to read documents in the collection.
Read the code snippets that use the document as follows:
<?php // 连接到mongodb $m = new MongoClient(); echo "Connection to database successfully"; // 选择一个数据库 $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; $cursor = $collection->find(); // 迭代显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
To execute the above procedure, the output is as follows:
Connection to database successfully Database mydb selected Collection selected succsessfully { "title": "MongoDB" }
Use the update() method to update the document.
The following example will update the title of the document as 'MongoDB Tutorial', and the code snippet is as follows:
<pre> <?php // 连接到mongodb $m = new MongoClient(); echo "Connection to database successfully"; // 选择一个数据库 $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; // 更新文档 $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB Tutorial"))); echo "Document updated successfully"; // 显示更新后的文档 $cursor = $collection->find(); // 循环显示文档标题 echo "Updated document"; foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
To execute the above procedure, the output is as follows:
Connection to database successfully Database mydb selected Collection selected succsessfully Document updated successfully Updated document { "title": "MongoDB Tutorial" }
Use the remove() method to delete the document.
In the following example, we will remove the data record with 'title' as 'MongoDB Tutorial'. , the code snippet is as follows:
<?php // 连接到mongodb $m = new MongoClient(); echo "Connection to database successfully"; // 选择一个数据库 $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; // 移除文档 $collection->remove(array("title"=>"MongoDB Tutorial"),false); echo "Documents deleted successfully"; // 显示可用文档数据 $cursor = $collection->find(); // iterate cursor to display title of documents echo "Updated document"; foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
To execute the above procedure, the output is as follows:
Connection to database successfully Database mydb selected Collection selected succsessfully Documents deleted successfully
In addition to the above examples, in php you can also use findOne(), save(), limit(), skip (), sort() and other methods to manipulate the Mongodb database.