MongoDB PHP
To use mongodb in php you must use the php driver of mongodb.
For the installation and driver package download of MongoDB PHP on various platforms, please see: PHP Installation of MongoDB Extension Driver
If you are using PHP7, please see: PHP7 MongoDB installation and use.
Ensure connection and select a database
In order to ensure correct connection, you need to specify the database name. If the database does not exist in mongoDB, mongoDB will automatically create it
The code snippet is as follows :
<?php $m = new MongoClient(); // 连接默认主机和端口为:mongodb://localhost:27017 $db = $m->test; // 获取名称为 "test" 的数据库 ?>
Create a collection
The code snippet to create a collection is as follows:
<?php $m = new MongoClient(); // 连接 $db = $m->test; // 获取名称为 "test" 的数据库 $collection = $db->createCollection("php"); echo "集合创建成功"; ?>
Execute the above program and the output result is as follows:
集合创建成功
Insert document
Use the insert() method in mongoDB to insert a document:
The code snippet for inserting a document is as follows:
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->php; // 选择集合 $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.php.cn/mongodb/", "by", "php中文网" ); $collection->insert($document); echo "数据插入成功"; ?>
Execute the above program and the output result is as follows:
数据插入成功
Then we use the db.php.find().pretty(); command on the mongo client to view the data:
Find documents
Use the find() method to read documents in the collection.
The code snippet for reading and using the document is as follows:
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->php; // 选择集合 $cursor = $collection->find(); // 迭代显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
Execute the above program, the output result is as follows:
MongoDB
Update the document
Use update () method to update the document.
The following example will update the title in the document to 'MongoDB Tutorial', the code snippet is as follows:
<pre> <?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->php; // 选择集合 // 更新文档 $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程"))); // 显示更新后的文档 $cursor = $collection->find(); // 循环显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
Execute the above program, the output result is as follows:
MongoDB 教程
Then we use mongo The client uses the db.php.find().pretty(); command to view the data:
Delete document
Use the remove() method to delete documents.
In the following example we will remove a data record whose 'title' is 'MongoDB Tutorial'. , The code snippet is as follows:
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->php; // 选择集合 // 移除文档 $collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true)); // 显示可用文档数据 $cursor = $collection->find(); foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
In addition to the above examples, you can also use findOne(), save(), limit(), skip(), sort() and other methods to operate the Mongodb database in php.
For more operation methods, please refer to the Mongodb core class: http://php.net/manual/zh/mongo.core.php.