PHP7 MongoDB API Usage _php Example

Source: Internet
Author: User
Tags api manual manual getmessage mongodb

Compile and install PHP7

Compile installation PHP7 mongdb extension

#先安装一个依赖库yum-y install openldap-develwget https://pecl.php.net/get/mongodb-1.1.1.tgz/home/server/php7/bin/phpize # Depends on your own compiled PHP environment./configure--with-php-config=/home/server/php7/bin/php-config make && make install# if successful, Generate a mongodb.so extension to modify php.ini configuration in lib/php/extensions/no-debug-non-zts-20151012/extension=mongodb.so

Note:

Previous versions used the mongo.so extension, the old Php-mongodb API
PHP7 has not been supported, at least for the time being.
The latest support PHP7 MongoDB only supports the new API (MongoDB > 2.6.X version) after compiling

Resources

github:https://github.com/mongodb/

Website:

http://www.mongodb.org/

PHP Official: Https://pecl.php.net/package/mongodb Http://pecl.php.net/package/mongo [Obsolete, currently only supported to PHP5.9999]

API Manual: Http://docs.php.net/manual/en/set.mongodb.php

Mongodb API Operations

Initializing MongoDB connections

$manager = new Mongodb/driver/manager ("mongodb://127.0.0.1:27017"); Var_dump ($manager);
Object (Mongodb/driver/manager) #1 (3) 
{ 
[request_id]]=> int (1714636915) 
[URI]=> string (25)] mongodb://localhost:27017 " 
[cluster"]=> array {  
["mode"]=>  string (6) "Direct"  
["state"] ]=>  String (4) "Born" 
["request_id"]=>  
int (0)  
["Sockettimeoutms"]=>  
int (300000)  
["Last_reconnect"]=>  
int (0)  
[URI]=>  
string] mongodb://localhost:27017  
[ Requires_auth "]=>  
int (0)  
[" Nodes "]=>  
Array (...)  
["Max_bson_size"]=>  
int (16777216)  
["Max_msg_size"]=>  
int (50331648)  
["Sec_latency_ms" ]=>  
int  
["Peers"]=>  
Array (0) {  
} 
["Replset"]=>  
NULL 
}}

Curl operation

$bulk = new Mongodb/driver/bulkwrite ([' Ordered ' => true]); $bulk->delete ([]);
$bulk->insert ([' _id ' => 1]);
$bulk->insert ([' _id ' => 2]); $bulk->insert ([' _id ' => 3, ' Hello ' => ' World ']) $bulk->update ([' _id ' => 3], [' $set ' => [' Hello '] =>
' Earth ']]);
$bulk->insert ([' _id ' => 4, ' hello ' => ' Pluto ']);
$bulk->update ([' _id ' => 4], [' $set ' => [' Hello ' => ' moon ']]);
$bulk->insert ([' _id ' => 3]);
$bulk->insert ([' _id ' => 4]);
$bulk->insert ([' _id ' => 5]);
$manager = new Mongodb/driver/manager (' mongodb://localhost:27017 ');
$writeConcern = new Mongodb/driver/writeconcern (mongodb/driver/writeconcern::majority, 1000); 
try {$result = $manager->executebulkwrite (' db.collection ', $bulk, $writeConcern);}  
catch (Mongodb/driver/exception/bulkwriteexception $e) {$result = $e->getwriteresult (); Check if the write concern could not is fulfilled if ($writeConcernError = $result->getwriteconcernerror ()) {print F ("%s (%d):%s/n ", $writeConcernError->getmessage (), $writeConcernError->getcode (), Var_export ($writeConcernErro 
R->getinfo (), true); }//Check If any write operations did not complete in all foreach ($result->getwriteerrors () as $writeError) {Prin   
TF ("operation#%d:%s (%d)/n", $writeError->getindex (), $writeError->getmessage (), $writeError->getcode ());  
The catch (mongodb/driver/exception/exception $e) {printf ("Other error:%s/n", $e->getmessage ()); Exit;}
printf ("Inserted%d document (s)/n", $result->getinsertedcount ()); printf ("Updated%d document (s)/n", $result->getmodifiedcount ());

Inquire

$filter = Array (); $options = Array (/* Only return  
the following fields in the matching documents * *  
"projection" => Array ("title" => 1, "article" => 1,  ),  
"sort" => Array ("views" =>-1,  ),  "modifiers" =& Gt Array (' $comment '  => "This is a query comment", ' $maxTimeMS ' =>  
),); $query = new Mongodb/driver/query ($ filter, $options); $manager = new Mongodb/driver/manager ("mongodb://localhost:27017");
$readPreference = new Mongodb/driver/readpreference (mongodb/driver/readpreference::rp_primary); $cursor = $manager- >executequery ("Databasename.collectionname", $query, $readPreference);
foreach ($cursor as $document) 
{ 
var_dump ($document);}

The above content is small to share the PHP7 MongoDB API use detailed, hope you like.

Related Article

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.