Php7 Mongdb Tutorial
This tutorial is only suitable for PHP7 environment. If you are using PHP5 environment, you can refer to (#).
## 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, 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 above command, the PHP7 installation directory is /usr/local/php7/. If you installed it in another directory, you need to modify the paths for pecl and php commands accordingly.
* * *
## MongoDB Usage
The PHP7 syntax for connecting to MongoDB is as follows:
$manager = new MongoDBDriverManager("mongodb://localhost:27017");
### Insert Data
Insert data with name "Tutorial" into the tutorial collection of the test database.
new MongoDBBSONObjectID, 'name' => '']; $_id= $bulk->insert($document); var_dump($_id); $manager = new MongoDBDriverManager("mongodb://localhost:27017"); $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.tutorial', $bulk, $writeConcern);?>
### Read Data
Here we insert three website data into the sites collection of the test database and read them iteratively:
insert(['x' => 1, 'name'=>'', 'url' => '']); $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 MongoDBDriverQuery($filter, $options); $cursor = $manager->executeQuery('test.sites', $query);foreach ($cursor as $document) { print_r($document);}?>
The output result is:
stdClass Object( => 3 => taobao => http://www.taobao.com) stdClass Object( => 2 => Google => http://www.google.com)
### Update Data
Next, we will update the data where x is 2 in the sites collection of the test database:
update( ['x' => 2], ['$set' => ['name' => 'Runoob Tools', 'url' => 'tool.']], ['multi' => false, 'upsert' => false]); $manager = new MongoDBDriverManager("mongodb://localhost:27017"); $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::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=2 has been changed to Tutorial Tools:
!(#)
### Delete Data
The following example deletes data where x is 1 and x is 2. Note the difference in the limit parameter:
delete(['x' => 1], ['limit' => 1]); // limit is 1 HourοΌDelete First Matching Data $bulk->delete(['x' => 2], ['limit' => 0]); // limit is 0 HourοΌDelete All Matching Data $manager = new MongoDBDriverManager("mongodb://localhost:27017"); $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);?>
For more usage methods, please refer to: [http://php.net/manual/en/book.mongodb.php](http://php.net/manual/en/book.mongodb.php).
YouTip