Compiling and installing PHP7
Compiling and installing PHP7 mongdb extensions
#先安装一个依赖库yum-y install openldap-develwget https://pecl.php.net/get/mongodb-1.1.1.tgz/home/server/php7/bin/phpize # Depending on your compiled PHP environment./configure--with-php-config=/home/server/php7/bin/php-config make && make install# if successful, Build a mongodb.so extension in lib/php/extensions/no-debug-non-zts-20151012/modify php.ini configuration extension=mongodb.so
Note:
The previous version was mongo.so extension, the old Php-mongodb API
The PHP7 has not been supported, at least not currently.
The latest support for PHP7 's MongoDB compilation only supports the new API (MongoDB > 2.6.X version)
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 [deprecated, currently only supported to PHP5.9999]
API Manual: Http://docs.php.net/manual/en/set.mongodb.php
Mongodb API Operations
Initializing a MongoDB connection
$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->updat E ([' _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, n); try {$result = $manager-& Gt;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 ()) {printf ( "%s (%d):%s/n", $writEconcernerror->getmessage (), $writeConcernError->getcode (), Var_export ($writeConcernError->getinfo (), true)); }//Check If any write operations do not complete at all foreach ($result->getwriteerrors () as $writeError) {printf ("operation#%d:%s (%d)/n", $writeError->getindex (), $writeError->getmessage (), $writeError->getcode ()); }} catch (Mongodb/driver/exception/exception $e) {printf ("Other error:%s/n", $e->getmessage ()); Exit;} printf ("Inserted%d document (s)/n", $result->getinsertedcount ());p rintf ("Updated%d document (s)/n", $result Getmodifiedcount ());
Inquire
$filter = Array (), $options = Array (/ * only return the following. 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 is a small series to share the PHP7 MongoDB API use detailed, I hope you like.