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 相关文章推荐
搜索和替换文件或目录的一个好类--很实用
Oct 09 PHP
用php的ob_start来生成静态页面的方法分析
Mar 09 PHP
php设计模式 Command(命令模式)
Jun 26 PHP
php中filter函数验证、过滤用户输入的数据
Jan 13 PHP
兼容ie6浏览器的php下载文件代码分享
Jul 14 PHP
thinkphp模板用法和内容输出实例
Nov 28 PHP
php实现encode64编码类实例
Mar 24 PHP
PHP CURL 多线程操作代码实例
May 13 PHP
Yii获取当前url和域名的方法
Jun 08 PHP
PHP给文字内容中的关键字进行套红处理
Apr 12 PHP
php实现映射操作实例详解
Oct 02 PHP
PHP上传图片到数据库并显示的实例代码
Dec 20 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制作静态网站的模板框架(四)
2006/10/09 PHP
PHP实现Soap通讯的方法
2014/11/03 PHP
基于PHP+jQuery+MySql实现红蓝(顶踩)投票代码
2015/08/25 PHP
Smarty模板变量调节器用法分析
2016/05/23 PHP
浅谈php和js中json的编码和解码
2016/10/24 PHP
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
2016/12/14 PHP
PHP基于GD2函数库实现验证码功能示例
2019/01/27 PHP
超级退弹代码
2008/07/07 Javascript
JavaScript 处理Iframe自适应高度(同或不同域名下)
2013/03/29 Javascript
JS比较两个时间大小的简单示例代码
2013/12/20 Javascript
jQuery简单实现禁用右键菜单
2015/03/10 Javascript
javascript结合canvas实现图片旋转效果
2015/05/03 Javascript
Javascript简写条件语句(推荐)
2016/06/12 Javascript
最全面的JS倒计时代码
2016/09/17 Javascript
js中动态创建json,动态为json添加属性、属性值的实例
2016/12/02 Javascript
本地存储localStorage用法详解
2017/07/31 Javascript
jQuery实现打开网页自动弹出遮罩层或点击弹出遮罩层功能示例
2017/10/19 jQuery
Vue.js组件通信的几种姿势
2017/10/23 Javascript
angular 实现的输入框数字千分位及保留几位小数点功能示例
2018/06/19 Javascript
vue项目前端知识点整理【收藏】
2019/05/13 Javascript
javascript将16进制的字符串转换为10进制整数hex
2020/03/05 Javascript
[03:02]2014DOTA2西雅图邀请赛 让队员自己告诉你DK NAVI备战情况
2014/07/08 DOTA
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
利用Python破解斗地主残局详解
2017/06/30 Python
Random 在 Python 中的使用方法
2018/08/09 Python
selenium使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
2018/11/29 Python
基于python的itchat库实现微信聊天机器人(推荐)
2019/10/29 Python
Python pip安装模块提示错误解决方案
2020/05/22 Python
玩转CSS3色彩
2010/01/16 HTML / CSS
CSS3中:nth-child和:nth-of-type的区别深入理解
2014/03/10 HTML / CSS
英智兴达软件测试笔试题
2016/10/12 面试题
高中生学习生活的自我评价
2013/11/27 职场文书
初一地理教学反思
2014/01/16 职场文书
芙蓉镇观后感
2015/06/10 职场文书
Spring Boot 排除某个类加载注入IOC的操作
2021/08/02 Java/Android
「月刊Action」2022年5月号封面公开
2022/03/21 日漫