Latest web development tutorials

MongoDB PHP

Use mongodb in php you must use the php mongodb drive.

MongoDB PHP installation on each platform and download the driver package please see: the PHP extension to install MongoDB driver

If you are using PHP7, please see: PHP7 the MongoDB installation and use .

Select a database and ensure that the connection

To ensure proper connection, you need to specify the database name, if the database does not exist in mongoDB, mongoDB automatically created

The following code fragment:

<?php
$m = new MongoClient(); // 连接默认主机和端口为:mongodb://localhost:27017
$db = $m->test; // 获取名称为 "test" 的数据库
?>

Create a collection

Create a collection of code snippets are as follows:

<?php
$m = new MongoClient(); // 连接
$db = $m->test; // 获取名称为 "test" 的数据库
$collection = $db->createCollection("w3big");
echo "集合创建成功";
?>

The above program, the output results are as follows:

集合创建成功

Insert Document

Use insert in mongoDB () method into the document:

Code snippets into the document as follows:

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->test;            // 选择一个数据库
$collection = $db->w3big; // 选择集合
$document = array( 
	"title" => "MongoDB", 
	"description" => "database", 
	"likes" => 100,
	"url" => "http://www.w3big.com/mongodb/",
	"by", "本教程"
);
$collection->insert($document);
echo "数据插入成功";
?>

The above program, the output results are as follows:

数据插入成功

Then we usedb.w3big.find () pretty () in the mongo client;command to view thedata:


Finding Documentation

Using the find () method to read a collection of documents.

Use the document read snippets are as follows:

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->test;            // 选择一个数据库
$collection = $db->w3big; // 选择集合

$cursor = $collection->find();
// 迭代显示文档标题
foreach ($cursor as $document) {
	echo $document["title"] . "\n";
}
?>

The above program, the output results are as follows:

MongoDB

Update Documentation

Use update () method to update the document.

The following example will update the document entitled 'MongoDB tutorial', the following code fragment:

<pre>
<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->test;            // 选择一个数据库
$collection = $db->w3big; // 选择集合
// 更新文档
$collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));
// 显示更新后的文档
$cursor = $collection->find();
// 循环显示文档标题
foreach ($cursor as $document) {
	echo $document["title"] . "\n";
}
?>

The above program, the output results are as follows:

MongoDB 教程

Then we usedb.w3big.find () pretty () in the mongo client;command to view thedata:


Deleting documents

Use remove () method to delete the document.

The following examples we will remove the 'title' is 'MongoDB tutorial' of a data record. , The following code fragment:

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->test;            // 选择一个数据库
$collection = $db->w3big; // 选择集合
   
// 移除文档
$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, in php you can also use findOne (), save (), limit (), skip (), sort () methods to manipulate Mongodb database.

More operations can refer Mongodb core classes: http://php.net/manual/zh/mongo.core.php .