PHP7之Mongodb API使用详解


Posted in PHP onDecember 26, 2015

编译安装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操作

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

PHP 相关文章推荐
Smarty+QUICKFORM小小演示
Feb 25 PHP
PHP5 操作MySQL数据库基础代码
Sep 29 PHP
php cookies中删除的一般赋值方法
May 07 PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
Sep 30 PHP
php中的路径问题与set_include_path使用介绍
Feb 11 PHP
PHP把小数转成整数3种方法
Jun 30 PHP
php实现ip白名单黑名单功能
Mar 12 PHP
PHP合并discuz用户脚本的方法
Aug 04 PHP
JSON字符串传到后台PHP处理问题的解决方法
Jun 05 PHP
利用PHP命令行模式采集股票趋势信息
Aug 09 PHP
Yii 2.0中场景的使用教程
Jun 02 PHP
解决Laravel使用验证时跳转到首页的问题
Nov 17 PHP
thinkPHP下的widget扩展用法实例分析
Dec 26 #PHP
thinkPHP下ueditor的使用方法详解
Dec 26 #PHP
thinkPHP中分页用法实例分析
Dec 26 #PHP
thinkPHP中验证码的简单使用方法
Dec 26 #PHP
分享50个提高PHP执行效率的技巧
Dec 26 #PHP
PHP获取二维数组中某一列的值集合
Dec 25 #PHP
PHP版本升级到7.x后wordpress的一些修改及wordpress技巧
Dec 25 #PHP
You might like
浅析PHP水印技术
2007/02/14 PHP
php daddslashes()和 saddslashes()有哪些区别分析
2012/10/26 PHP
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
2014/05/04 PHP
判断一个变量是数组Array类型的方法
2013/09/16 Javascript
3种Jquery限制文本框只能输入数字字母的方法
2014/12/03 Javascript
jquery实现聚光灯效果的方法
2015/02/06 Javascript
jQuery表单域属性过滤器用法分析
2015/02/10 Javascript
javascript手工制作悬浮菜单
2015/02/12 Javascript
jQuery+jRange实现滑动选取数值范围特效
2015/03/14 Javascript
jquery.cookie.js用法实例详解
2015/12/25 Javascript
非常实用的js验证框架实现源码 附原理方法
2016/06/08 Javascript
实例讲解JavaScript中的this指向错误解决方法
2016/06/13 Javascript
Laydate时间组件在火狐浏览器下有多时间输入框时只能给第一个输入框赋值的解决方法
2016/08/18 Javascript
Vue项目webpack打包部署到服务器的实例详解
2017/07/17 Javascript
[原创]jQuery实现合并/追加数组并去除重复项的方法
2018/04/11 jQuery
js与jQuery实现获取table中的数据并拼成json字符串操作示例
2018/07/12 jQuery
在vue中安装使用vux的教程详解
2018/09/16 Javascript
nodejs文件夹深层复制功能
2019/09/03 NodeJs
详解vue-router 动态路由下子页面多页共活的解决方案
2019/12/22 Javascript
node.js中module模块的功能理解与用法实例分析
2020/02/14 Javascript
Python的设计模式编程入门指南
2015/04/02 Python
如何处理Python3.4 使用pymssql 乱码问题
2016/01/08 Python
Python脚本处理空格的方法
2016/08/08 Python
Python中__init__.py文件的作用详解
2016/09/18 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
Python+OpenCV 实现图片无损旋转90°且无黑边
2019/12/12 Python
简单的HTML5初步入门教程
2015/09/29 HTML / CSS
DJI美国:消费类无人机领域的领导者
2018/04/27 全球购物
英国皇家造币厂:The Royal Mint
2018/10/05 全球购物
英国在线药房和在线医生:LloydsPharmacy
2019/10/21 全球购物
促销活动总结报告
2014/04/26 职场文书
大学生个人学年总结
2015/02/15 职场文书
会计主管岗位职责
2015/04/02 职场文书
2015年试用期工作总结范文
2015/05/28 职场文书
导游词之徐州云龙湖
2019/11/19 职场文书
Go获取两个时区的时间差
2022/04/20 Golang