欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP7之Mongodb API使用详解

程序员文章站 2023-09-08 09:15:39
编译安装php7 编译安装php7 mongdb扩展 #先安装一个依赖库yum -y install openldap-develwget /home/serve...

编译安装php7

编译安装php7 mongdb扩展

#先安装一个依赖库yum -y install openldap-develwget /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:

官网:

php官方: [已废弃,目前只支持到php5.9999]

api手册:

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操作

$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 be fulfilled  
if ($writeconcernerror = $result->getwriteconcernerror())
{printf("%s (%d): %s/n",  
$writeconcernerror->getmessage(),  
$writeconcernerror->getcode(),  
var_export($writeconcernerror->getinfo(), true)); 
}  
// check if any write operations did 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());
printf("updated %d document(s)/n", $result->getmodifiedcount());

查询

$filter = array();$options = array(  
/* only return the following fields in the matching documents */  
"projection" => array("title" => 1,"article" => 1,  ),  
"sort" => array("views" => -1,  ),  "modifiers" => array('$comment'  => "this is a query comment",'$maxtimems' => 100,  
),);$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);}

以上内容是小编给大家分享的php7之mongodb api使用详解,希望大家喜欢。