mongo Table类文件 获取MongoCursor(游标)的实现方法分析


Posted in PHP onJuly 01, 2013

MongoCursor Object
游标类

Mongo
Config.php配置文件
Table.php(mongodb操作数据库类文件)

Config.php配置文件

<?php
require_once 'Zend/Exception.php';
class Hrs_Mongo_Config
{
    const VERSION = '1.7.0';
    const DEFAULT_HOST = 'localhost';
    const DEFAULT_PORT = 27017;
    private static $host = self::DEFAULT_HOST ;
    private static $port = self::DEFAULT_PORT ;
    private static $options = array(
            'connect' => true,
            'timeout' => 30,
            //'replicaSet' => '' //If this is given, the master will be determined by using the ismaster database command on the seeds
    );
    public static $conn = '';
    public static $defaultDb = '';
    public static $linkStatus = '';
    public static function set($server = 'mongodb://localhost:27017', $options = array('connect' => true)) {
        if(!$server){
            $url = 'mongodb://'.self::$host.':'.self::$port;
        }
        if(is_array($server)){
            if(isset($server['host'])){
                self::$host = $server['host'];
            }
            if(isset($server['port'])){
                self::$port = $server['port'];
            }
            if(isset($server['user']) && isset($server['pass'])){
                $url = 'mongodb://'.$server['user'].':'.$server['pass'].'@'.self::$host.':'.self::$port;
            }else{
                $url = 'mongodb://'.self::$host.':'.self::$port;
            }
        }
        if(is_array($options)){
            foreach (self::$options as $o_k=>$o_v){
                if(isset($options[$o_k]))
                    self::$options[$o_k] = $o_v;
            }
        }
        try{                        
            self::$conn = new Mongo($url, self::$options);
            self::$linkStatus = 'success';
        }catch (Exception $e){
            self::$linkStatus = 'failed';
        }
        if(isset($server['database'])){
            self::selectDB($server['database']);
        }
    }
    public static function selectDB($database){
        if($database){
            try {
                if(self::$linkStatus=='success')
                    self::$defaultDb = self::$conn->selectDB($database);
                return self::$defaultDb;
            }
            catch(InvalidArgumentException $e) {
                throw new Zend_Exception('Mongodb数据库名称不正确');
            }
        }else{
            throw new Zend_Exception('Mongodb数据库名称不能为空');
        }
    }
}

Table.php(mongodb操作数据库类文件)
<?php
require_once 'Hrs/Mongo/Config.php';
abstract class Hrs_Mongo_Table
{
    protected $_db = '';
    protected $_name = '';
    protected $_data = array();
    protected $c_options = array(
            'fsync'=>true,
            'safe'=>true
    );
    protected $u_options = array(
    //'upsert'=>false,
            'multiple'=>true,
            'fsync'=>true,
            'safe'=>true
    );
    /*
     protected $r_options = array(
     );*/
    protected $d_options = array(
            'fsync'=>true,
            'justOne'=>false,
            'safe'=>true
    );
    protected function _setAdapter($database=''){
        if(!$database)
            throw new Zend_Exception('Mongodb数据库名称不能为空');
        Hrs_Mongo_Config::selectDB($database);
    }
    public function __construct() {
        if(Hrs_Mongo_Config::$conn instanceof Mongo){
            $name = $this->_name;
            $defDb = Hrs_Mongo_Config::$defaultDb;
            $this->_db = $defDb->$name;
        }else{
            throw new Zend_Exception('Mongodb服务器连接失败');
        }
    }
    public function insert($data){
        if(!$this->testLink()) return false;
        $ret = $this->_db->insert($data, $this->c_options);
        return $ret;
    }
    public function update($data, $where){
        if(!$this->testLink()) return false;
        return $this->_db->update($where, $data, $this->u_options);
    }
    public function find($where=array(),$limit=0){
        if($this->testLink()) {
            if($limit>0){
                $this->_data = $where ? $this->_db->find($where)->limit($limit)->snapshot() : $this->_db->find()->limit($limit)->snapshot();
            }else{
                $this->_data = $where ? $this->_db->find($where)->limit($limit)->snapshot() : $this->_db->find()->limit($limit)->snapshot();
            }
        }
        return $this;
    }
    //find cursor
    /*
     * 获取游标对象
     */
    public function look($where=array(),$fields=array()){
        if($this->testLink()) {
            if($fields){
                return $where ? $this->_db->find($where,$fields): $this->_db->find()->fields($fields);
            }else{
                return $where ? $this->_db->find($where) : $this->_db->find();
            }
        }
        return false;
    }
    public function delete($where){
        if(!$this->testLink()) return false;
        return $this->_db->remove($where, $this->d_options);
    }
    public function dropMe(){
        if(!$this->testLink()) return false;
        return $this->_db->drop();
    }
    public function __toString(){
        return $this->_data;
    }
    public function toArray(){
        $tmpData = array();
        foreach($this->_data as $id=>$row){
            $one_row = array();
            foreach($row as $key=>$col){
                $one_row[$key] = $col;
            }
            $one_row['_id'] = $id;
            $tmpData[] = $one_row;
        }
        return $tmpData;
    }
    protected function testLink(){
        return Hrs_Mongo_Config::$linkStatus == 'success' ? true :false;
    }
}

要点注意!!!
第一种方法
    //find cursor
    /*
     * 获取游标对象
     */
    public function look($where=array(),$fields=array()){
        if($this->testLink()) {
            if($fields){
                return $where ? $this->_db->find($where,$fields): $this->_db->find()->fields($fields);
            }else{
                return $where ? $this->_db->find($where) : $this->_db->find();
            }
        }
        return false;
    }

第二种方法
    public function find($where=array(),$field=array()){
        if($this->testLink()) {
            $this->_data = $this->_db->find($where,$field)->sort(array("_id" => -1));
        }
        return $this;
    }

    /*
     * 获取游标对象
     */
    public function getCursor(){
     return $this->_data;
    }

第二种需要的是find得到的不是数组
find($where)->getCursor();是MongoCursor Object

注意注意
find()返回的是当前对象
toArray()方法是把当前对象转换为数组
getCursor()方法是把当前对象转换为MongoCursor Object(游标对象)
PHP 相关文章推荐
一次编写,随处运行
Oct 09 PHP
php采集速度探究总结(原创)
Apr 18 PHP
php下图片文字混合水印与缩略图实现代码
Dec 11 PHP
php懒人函数 自动添加数据
Jun 28 PHP
php中通过curl smtp发送邮件
Jun 05 PHP
php定时删除文件夹下文件(清理缓存文件)
Jan 23 PHP
PHP面向对象之旅:深入理解static变量与方法
Jan 06 PHP
php传值赋值和传地址赋值用法实例分析
Jun 20 PHP
使用Composer安装Yii框架的方法
Mar 15 PHP
PHP实现JS中escape与unescape的方法
Jul 11 PHP
PHP 传输会话curl函数的实例详解
Sep 12 PHP
PHP 实现 JSON 数据的编码和解码操作详解
Apr 22 PHP
php5.3 注意事项说明
Jul 01 #PHP
file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍
Jul 01 #PHP
如何给phpcms v9增加类似于phpcms 2008中的关键词表
Jul 01 #PHP
解析php做推送服务端实现ios消息推送
Jul 01 #PHP
php ios推送(代码)
Jul 01 #PHP
PHP分页效率终结版(推荐)
Jul 01 #PHP
解析php防止form重复提交的方法
Jul 01 #PHP
You might like
PHP PDO函数库详解
2010/04/27 PHP
保存到桌面、设为桌面且带图标的PHP代码
2013/11/19 PHP
ThinkPHP使用Ueditor的方法详解
2016/05/20 PHP
php微信公众号开发之校园图书馆
2018/10/20 PHP
PHP实现的多进程控制demo示例
2019/07/22 PHP
用jquery与css打造个性化的单选框和复选框
2010/10/20 Javascript
document.write()及其输出内容的样式、位置控制
2013/08/12 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
js简单实现交换Li的值
2014/05/22 Javascript
vue.js学习笔记之绑定style样式和class列表
2016/10/31 Javascript
基于jQuery实现数字滚动效果
2017/01/16 Javascript
vue父子组件的数据传递示例
2017/03/07 Javascript
用jQuery实现圆点图片轮播效果
2017/03/19 Javascript
vue.js指令和组件详细介绍及实例
2017/04/06 Javascript
React 子组件向父组件传值的方法
2017/07/24 Javascript
webstorm中配置nodejs环境及npm的实例
2018/05/15 NodeJs
使用Python实现下载网易云音乐的高清MV
2015/03/16 Python
python模拟登录并且保持cookie的方法详解
2017/04/04 Python
numpy判断数值类型、过滤出数值型数据的方法
2018/06/09 Python
Python读取指定日期邮件的实例
2019/02/01 Python
Numpy的简单用法小结
2019/08/28 Python
Python pandas对excel的操作实现示例
2020/07/21 Python
pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异
2021/02/25 Python
纯CSS3实现的阴影效果
2014/12/24 HTML / CSS
css3实现平移效果(transfrom:translate)的示例
2020/11/13 HTML / CSS
最好的商品表达自己:Cafepress
2019/09/04 全球购物
马来西亚在线药房:RoyalePharma
2019/12/01 全球购物
Java TransactionAPI (JTA) 主要包含几部分
2012/12/07 面试题
审计工作个人的自我评价
2013/12/25 职场文书
财务主管自我鉴定
2014/01/17 职场文书
租房合同协议书
2014/04/09 职场文书
《海伦?凯勒》教学反思
2014/04/17 职场文书
医院护士党的群众路线教育实践活动对照检查材料思想汇报
2014/10/04 职场文书
2014年有孩子的离婚协议书范本
2014/10/08 职场文书
2015年食堂工作总结报告
2015/04/23 职场文书
《卖火柴的小女孩》教学反思
2016/02/19 职场文书