PHP7 MongDB is installed and used

This tutorial is only suitable for PHP7 environments, if you are a PHP5 environment, you can refer to PHP MongDB installation and use.

PHP7 Mongdb extended installation

We use the pecl command to install:

$ /usr/local/php7/bin/pecl install mongodb

After a successful execution, the following results are 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, we open the php .ini file and add the extension-mongodb.so configuration.

You can add the following command directly.

$ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

Note: The installation directory for php7 in the command executed above is /usr/local/php7/, and if you install it in another directory, you need to modify the path between pecl and php command accordingly.


Mongodb is used

The PHP7 connection MongoDB syntax is as follows:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

Insert the data

Insert data named "W3Cschool Tutorial" into the w3cschool collection of the test database.

<?php
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'W3Cschool教程'];

$_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.w3cschool', $bulk, $writeConcern);
?>

Read the data

Here we insert three URL data into the sites collection of the test database and read the iteration out:

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  

// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'W3Cschool教程', 'url' => 'http://www.w3cschool.cn']);
$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 is:

stdClass Object
(
    [x] => 3
    [name] => taobao
    [url] => http://www.taobao.com
)
stdClass Object
(
    [x] => 2
    [name] => Google
    [url] => http://www.google.com
)

Update the data

Next we'll update the data in the test database sites collection x to 2:

<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
    ['x' => 2],
    ['$set' => ['name' => 'w3cschool在线工具', 'url' => '123.w3cschool.cn/webtools']],
    ['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 see how the data has changed, and the data x for 2 has become the w3cschool online tool:

MongDB PHP7

Delete the data

The following instance removes data with x for 1 and x for 2, noting the difference between the limit parameters:

<?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 information, please refer to: http://php.net/manual/en/book.mongodb.php.