這篇文章主要介紹了PHP7之Mongodb API使用詳解的相關資料,須要的朋友能夠參考下php
譯安裝PHP7node
編譯安裝PHP7 Mongdb擴展git
#先安裝一個依賴庫yum -y install openldap-develwget https://pecl.php.net/get/mongodb-1.1.1.tgz /home/server/php7/bin/phpize #根據本身編譯的PHP環境而定./configure --with-php-config=/home/server/php7/bin/php-config make && make install#若是成功,生成一個mongodb.so擴展在lib/php/extensions/no-debug-non-zts-20151012/修改php.ini配置extension=mongodb.sogithub
注:mongodb
之前版本用的是mongo.so擴展,老的php-mongodb api
在PHP7已經不支持了,至少目前不支持。
最新支持PHP7的mongodb 編譯後 僅支持新版API(mongodb > 2.6.X版本)api
參考資料php7
GITHUB: https://github.com/mongodb/socket
官網:ui
PHP官方: https://pecl.php.net/package/mongodb http://pecl.php.net/package/mongo [已廢棄,目前只支持到PHP5.9999]
API手冊:http://docs.php.net/manual/en/set.mongodb.php
Mongodb API 操做
初始化Mongodb鏈接
$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(13) { ["mode"]=> string(6) "direct" ["state"]=> string(4) "born" ["request_id"]=> int(0) ["sockettimeoutms"]=> int(300000) ["last_reconnect"]=> int(0) ["uri"]=> string(25) "mongodb://localhost:27017" ["requires_auth"]=> int(0) ["nodes"]=> array(...) ["max_bson_size"]=> int(16777216) ["max_msg_size"]=> int(50331648) ["sec_latency_ms"]=> int(15) ["peers"]=> array(0) { } ["replSet"]=> NULL }}
CURL操做
1 $bulk = new MongoDB/Driver/BulkWrite(['ordered' => true]);$bulk->delete([]); 2 $bulk->insert(['_id' => 1]); 3 $bulk->insert(['_id' => 2]); 4 $bulk->insert(['_id' => 3, 5 'hello' => 'world']);$bulk->update(['_id' => 3], 6 ['$set' => ['hello' => 'earth']]); 7 $bulk->insert(['_id' => 4, 'hello' => 'pluto']); 8 $bulk->update(['_id' => 4], ['$set' => ['hello' => 'moon']]); 9 $bulk->insert(['_id' => 3]); 10 $bulk->insert(['_id' => 4]); 11 $bulk->insert(['_id' => 5]); 12 $manager = new MongoDB/Driver/Manager('mongodb://localhost:27017'); 13 $writeConcern = new MongoDB/Driver/WriteConcern(MongoDB/Driver/WriteConcern::MAJORITY, 1000); 14 try { 15 $result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern); 16 } 17 catch (MongoDB/Driver/Exception/BulkWriteException $e) 18 { 19 $result = $e->getWriteResult(); 20 // Check if the write concern could not be fulfilled 21 if ($writeConcernError = $result->getWriteConcernError()) 22 {printf("%s (%d): %s/n", 23 $writeConcernError->getMessage(), 24 $writeConcernError->getCode(), 25 var_export($writeConcernError->getInfo(), true)); 26 } 27 // Check if any write operations did not complete at all 28 foreach ($result->getWriteErrors() as $writeError) {printf("Operation#%d: %s (%d)/n", 29 $writeError->getIndex(), 30 $writeError->getMessage(), 31 $writeError->getCode()); 32 }} catch (MongoDB/Driver/Exception/Exception $e) 33 { 34 printf("Other error: %s/n", $e->getMessage()); 35 exit;}printf("Inserted %d document(s)/n", $result->getInsertedCount()); 36 printf("Updated %d document(s)/n", $result->getModifiedCount());
查詢
1 $filter = array();$options = array( 2 /* Only return the following fields in the matching documents */ 3 "projection" => array("title" => 1,"article" => 1, ),
"sort" => array("views" => -1, ),
"modifiers" => array('$comment' => "This is a query comment",'$maxTimeMS' => 100));
4 $query = new MongoDB/Driver/Query($filter, $options);
5 $manager = new MongoDB/Driver/Manager("mongodb://localhost:27017"); 6 $readPreference = new MongoDB/Driver/ReadPreference(MongoDB/Driver/ReadPreference::RP_PRIMARY);
7 $cursor = $manager->executeQuery("databaseName.collectionName", $query, $readPreference); 8 foreach($cursor as $document){ 9 var_dump($document);
10 }
以上內容是小編給你們分享的PHP7之Mongodb API使用詳解,但願你們喜歡。