PHP7 MONGDB Installation and use

Source: Internet
Author: User
Tags install mongodb

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.