Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP7 MongDB Installation and Usage

PHP7 MongDB Installation and Usage

This tutorial is only suitable for the PHP7 environment. If you are using PHP5, you can refer to PHP MongDB Installation and Usage。

PHP7 Mongdb Extension Installation

We use the pecl command to install:



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

After successful execution, the following result is 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 extension=mongodb.so configuration.

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 commands executed above, 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 connecting to MongoDB in PHP7 is as follows:



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

Insert data

Insert the data with name "Runoob Tutorial" into the runoob collection of the test database.



 new MongoDB\BSON\ObjectID, 'name' => 'Runoob Tutorial'];



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

?>

Read data

Here we insert three website data into the sites collection of the test database, and read and iterate them out:



insert(['x' => 1, 'name'=>'Runoob Tutorial', 'url' => 'http://www.runoob.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 data

$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 data

Next we will update the data with x = 2 in the sites collection of the test database:



update(

    ['x' => 2],

    ['$set' => ['name' => '菜鸟工具', 'url' => 'tool.runoob.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 changes in the data. The data with x = 2 has become Runoob Tool:

Delete data

The following example deletes the data with x = 1 and x = 2. Note the difference in the limit parameter:



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