We use the PECL command to install:
/usr/local/php7/bin/pecl Install MongoDB
After successful execution, the following results are output:
'/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so 'install OK: Channel:// pecl.php.net/mongodb-1.1.7configuration option "Php_ini" is not set to PHP. "extension=mongodb.so" to PHP.ini
Next we open the php.ini file and add the extension=mongodb.so configuration.
You can add them directly by executing the following command.
Echo "Extension=mongodb.so" >> '/usr/local/php7/bin/php--ini | grep "Loaded Configuration" | Sed-e "s|. *:\s*| | " `
Note: The above command in the PHP7 installation directory is/usr/local/php7/, if you install in another directory, you need to modify pecl with the path of the PHP command.
Mongodb use
The PHP7 connection MongoDB syntax is as follows:
$manager New Mongodb\driver\manager ("mongodb://localhost:27017");
Inserting data
Insert the data with name "Baidu" into the Runoob collection of the test database.
<?PHP$bulk=NewMongodb\driver\bulkwrite;$document= [' _id ' + =NewMongodb\bson\objectid, ' name ' = ' Baidu '];$_id=$bulk->insert ($document);Var_dump($_id);$manager=NewMongodb\driver\manager ("mongodb://localhost:27017"); $writeConcern=NewMongodb\driver\writeconcern (mongodb\driver\writeconcern::majority, 1000);$result=$manager->executebulkwrite (' Test.runoob ',$bulk,$writeConcern);?>
Reading data
Here we insert three URL data into the sites collection of the test database and read the iteration out:
<?PHP$manager=NewMongodb\driver\manager ("mongodb://localhost:27017"); //Inserting Data$bulk=NewMongodb\driver\bulkwrite;$bulk->insert ([' x ' = + 1, ' name ' = + ' Baidu ', ' url ' = ' http://www.baidu.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],];//Querying Data$query=NewMongodb\driver\query ($filter,$options);$cursor=$manager->executequery (' Test.sites ',$query);foreach($cursor as $document) { Print_r($document);}?>
The output is:
Object ( = = 3 = Taobao= http://www.taobao.com Object( =2 = Google + http:// www.google.com)
PHP7 MONGDB Installation and use