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文件
Jan 04 PHP
thinkphp3查询mssql数据库乱码解决方法分享
Feb 11 PHP
ThinkPHP之A方法实例讲解
Jun 20 PHP
php实现的树形结构数据存取类实例
Nov 29 PHP
2款PHP无限级分类实例代码
Nov 11 PHP
实现PHP框架系列文章(6)mysql数据库方法
Mar 04 PHP
PHP 返回13位时间戳的实现代码
May 13 PHP
php求今天、昨天、明天时间戳的简单实现方法
Jul 28 PHP
Symfony2获取web目录绝对路径、相对路径、网址的方法
Nov 14 PHP
PHP多进程编程之僵尸进程问题的理解
Oct 15 PHP
php使用QueryList轻松采集js动态渲染页面方法
Sep 11 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文章按日期(月日)SQL归档语句
2012/11/29 PHP
CI框架源码解读之利用Hook.php文件完成功能扩展的方法
2016/05/18 PHP
php实现有序数组打印或排序的方法【附Python、C及Go语言实现代码】
2016/11/10 PHP
老生常谈php 正则中的i,m,s,x,e分别表示什么
2017/03/02 PHP
解javascript 混淆加密收藏
2009/01/16 Javascript
js 省地市级联选择
2010/02/07 Javascript
幻灯片带网页设计中的20个奇妙应用示例小结
2012/05/27 Javascript
jquery多行滚动/向左或向上滚动/响应鼠标实现思路及代码
2013/01/23 Javascript
jquery简单实现滚动条下拉DIV固定在头部不动
2013/11/25 Javascript
jquery垂直公告滚动实现代码
2013/12/08 Javascript
JavaScript判断是否为数字的4种方法及效率比较
2015/04/01 Javascript
jQuery三级下拉列表导航菜单代码分享
2020/04/15 Javascript
jQuery on()绑定动态元素出现的问题小结
2016/02/19 Javascript
jQuery获取多种input值的简单实现方法
2016/06/20 Javascript
JS实现动态添加DOM节点和事件的方法示例
2017/04/28 Javascript
Python通过解析网页实现看报程序的方法
2014/08/04 Python
Python 高级专用类方法的实例详解
2017/09/11 Python
Python实现的寻找前5个默尼森数算法示例
2018/03/25 Python
Python + selenium自动化环境搭建的完整步骤
2018/05/19 Python
在Python中使用defaultdict初始化字典以及应用方法
2018/10/31 Python
对python过滤器和lambda函数的用法详解
2019/01/21 Python
Python Pillow Image Invert
2019/01/22 Python
对Python 简单串口收发GUI界面的实例详解
2019/06/12 Python
Python多线程及其基本使用方法实例分析
2019/10/29 Python
python 基于opencv 绘制图像轮廓
2020/12/11 Python
CSS3 简写animation
2012/05/10 HTML / CSS
德国古洛迷亚百货官网:GALERIA Kaufhof
2017/06/20 全球购物
通信专业个人自我鉴定
2013/10/21 职场文书
学生手册家长评语
2014/02/10 职场文书
文化活动实施方案
2014/03/28 职场文书
装饰工程师岗位职责
2014/06/08 职场文书
《中国梦我的梦》小学生演讲稿
2014/08/20 职场文书
做一个有道德的人活动方案
2014/08/25 职场文书
个人职业生涯规划之自我评估篇
2019/09/03 职场文书
浅谈Redis 中的过期删除策略和内存淘汰机制
2022/04/03 Redis
什么是clearfix (一文搞清楚css清除浮动clearfix)
2023/05/21 HTML / CSS