PHP7之Mongodb API使用详解


这篇文章主要介绍了PHP7之Mongodb API使用详解的相关资料,需要的朋友可以参考下

译安装PHP7

编译安装PHP7 Mongdb扩展

#先安装一个依赖库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.so

注:

以前版本用的是mongo.so扩展,老的php-mongodb api
在PHP7已经不支持了,至少目前不支持。
最新支持PHP7的mongodb 编译后 仅支持新版API(mongodb > 2.6.X版本)

参考资料

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

官网:

http://www.mongodb.org/

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使用详解,希望大家喜欢。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM