PHP7 MongoDB Installation and Usage
This tutorial is only applicable to PHP7 environment. If you are using PHP5, you can refer toPHP MongoDB Installation and Usage。
PHP7 MongoDB Extension Installation
We use the pecl command to install:
$ /usr/local/php7/bin/pecl install mongodb
After successful execution, the following results will be output:
…… Build process completed successfully Installing '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so' install ok: channel://pecl.php.net/mongodb-1.1.7 configuration option "php_ini" is not set to php.ini location You should add "extension=mongodb.so" to php.ini
Next, open the php.ini file and addextension=mongodb.soconfiguration.
You can directly execute the following command to add it.
$ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
Note:In the above commands, the installation directory of php7 is /usr/local/php7/. If you installed it in another directory, you need to modify the paths of the pecl and php commands accordingly.
MongoDB Usage
The syntax for PHP7 to connect to MongoDB is as follows:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
Insert Data
Insert the data with name "Example" into the example collection of the test database.
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'Example'];
$_id= $bulk->insert($document);
var_dump($_id);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.example', $bulk, $writeConcern);
?>
Read Data
Here we insert three URL data into the sites collection of the test database, and read and iterate them out:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'Example', 'url' => 'http://www.example.com']);
$bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk);
$filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1],
];
// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.sites', $query);
foreach ($cursor as $document) {
print_r($document);
}
?>
The output result is:
stdClass Object
(
[x] => 3
[name] => taobao
[url] => http://www.taobao.com
)
stdClass Object
(
[x] => 2
[name] => Google
[url] => http://www.google.com
)
Update Data
Next, we will update the data with x equal to 2 in the sites collection of the test database:
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['x' => 2],
['$set' => ['name' => 'Example工具', 'url' => 'tool.example.com']],
['multi' => false, 'upsert' => false]
);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>
Next, we use the "db.sites.find()" command to view the data changes. The data with x equal to 2 has changed to Example Tool:

Delete Data
The following example deletes the data with x equal to 1 and x equal to 2. Note the difference in the limit parameter:
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['x' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据
$bulk->delete(['x' => 2], ['limit' => 0]); // limit 为 0 时,删除所有匹配数据
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>
For more usage, please refer to:http://php.net/manual/en/book.mongodb.php。
Other Extensions