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 相关文章推荐
PHP 和 MySQL 基础教程(一)
Oct 09 PHP
sourcesafe管理phpproj文件的补充说明(downmoon)
Apr 11 PHP
最新用php获取谷歌PR值算法,附上php查询PR值代码示例
Dec 25 PHP
php教程 插件机制在PHP中实现方案
Nov 02 PHP
php中spl_autoload详解
Oct 17 PHP
PHP调用wsdl文件类型的接口代码分享
Nov 19 PHP
PHP魔术方法__GET、__SET使用实例
Nov 25 PHP
php+mysql实现无限分类实例详解
Jan 15 PHP
Yii视图CGridView实现操作按钮定义地址示例
Jul 14 PHP
微信公众平台开发-微信服务器IP接口实例(含源码)
Mar 05 PHP
PHP反射学习入门示例
Jun 14 PHP
PHP rmdir()函数的用法总结
Jul 02 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 压缩文件夹的类代码
2009/11/05 PHP
PHP开发的一些注意点总结
2010/10/12 PHP
PHP捕获Fatal error错误的方法
2014/06/11 PHP
PHP多进程编程实例
2014/10/15 PHP
PHP has encountered a Stack overflow问题解决方法
2014/11/03 PHP
Yii2.0预定义的别名功能小结
2016/07/04 PHP
PHP批量删除jQuery操作
2017/07/23 PHP
javascript比较文档位置
2008/04/08 Javascript
javascript eval函数深入认识
2009/02/21 Javascript
jQuery 可以拖动的div实现代码 脚本之家修正版
2009/06/26 Javascript
载入jQuery库的最佳方法详细说明及实现代码
2012/12/28 Javascript
window.location 对象所包含的属性
2014/10/10 Javascript
jQuery选择器源码解读(五):tokenize的解析过程
2015/03/31 Javascript
jQuery基于ID调用指定iframe页面内的方法
2016/07/06 Javascript
jquery实现下拉框左右选择功能
2017/02/21 Javascript
JavaScript中document.referrer的用法详解
2017/07/04 Javascript
利用纯js + transition动画实现移动端web轮播图详解
2017/09/10 Javascript
express+mockjs实现模拟后台数据发送功能
2018/01/07 Javascript
React从react-router路由上做登陆验证控制的方法
2018/05/10 Javascript
详解webpack4多入口、多页面项目构建案例
2018/05/25 Javascript
从组件封装看Vue的作用域插槽的实现
2019/02/12 Javascript
JS把字符串格式的时间转换成几秒前、几分钟前、几小时前、几天前等格式
2019/07/10 Javascript
JavaScript Dom 绑定事件操作实例详解
2019/10/02 Javascript
javascript实现时钟动画
2020/12/03 Javascript
Python 备份程序代码实现
2017/03/06 Python
python算法演练_One Rule 算法(详解)
2017/05/17 Python
Python实现的桶排序算法示例
2017/11/29 Python
python互斥锁、加锁、同步机制、异步通信知识总结
2018/02/11 Python
pandas DataFrame数据转为list的方法
2018/04/11 Python
idea创建springMVC框架和配置小文件的教程图解
2018/09/18 Python
h5移动端调用支付宝、微信支付的实现
2020/06/08 HTML / CSS
应届毕业生自荐信例文
2014/02/26 职场文书
博士生导师推荐信
2014/07/08 职场文书
十八大宣传标语
2014/10/09 职场文书
临时工聘用合同协议书
2014/10/29 职场文书
六年级语文教学反思
2016/03/03 职场文书