PHP7 MongoDB Installation and Usage
This tutorial is only suitable for PHP7 environments. If you are using PHP5, you can refer to PHP 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 result 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, we open the php.ini file and add the 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: The installation directory for php7 in the above commands is /usr/local/php7/. If you have installed it in another directory, you need to modify the paths for the pecl and php commands accordingly.
MongoDB Usage
The syntax for connecting to MongoDB with PHP7 is as follows:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
Inserting Data
Insert data with the name "tutorialpro.org" into the tutorialpro collection of the test database.
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'tutorialpro.org'];
$_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.tutorialpro', $bulk, $writeConcern);
?>
Reading Data
Here, we insert three website data into the sites collection of the test database and read them iteratively:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// Insert data
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'tutorialpro.org', 'url' => 'http://www.tutorialpro.org']);
$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);
}
?>
Output result:
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 where x is 2 in the sites collection of the test database:
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['x' => 2],
['$set' => ['name' => 'tutorialpro', 'url' => 'tool.tutorialpro.org']],
['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 the changes in the data, and the data where x is 2 has been changed to tutorialpro:
Delete Data
The following example deletes data where x is 1 and x is 2, note the difference in the limit parameter:
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['x' => 1], ['limit' => 1]); // When limit is 1, delete the first matching data
$bulk->delete(['x' => 2], ['limit' => 0]); // When limit is 0, delete all matching data
$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.