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 相关文章推荐
PHP4实际应用经验篇(7)
Oct 09 PHP
php 操作调试的方法
Jul 12 PHP
析构函数与php的垃圾回收机制详解
Oct 28 PHP
php的慢速日志引起的Mysql错误问题分析
May 13 PHP
ThinkPHP3.1新特性之多数据库操作更加完善
Jun 19 PHP
php利用cookies实现购物车的方法
Dec 10 PHP
PHP读取配置文件类实例(可读取ini,yaml,xml等)
Jul 28 PHP
thinkphp跨库操作的简单代码实例
Sep 22 PHP
ThinkPHP框架整合微信支付之JSAPI模式图文详解
Apr 09 PHP
phpstudy后门rce批量利用脚本的实现
Dec 12 PHP
php与阿里云短信接口接入操作案例分析
May 27 PHP
如何用Laravel包含你自己的帮助函数
May 27 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命令行脚本接收传入参数的三种方式
2014/08/20 PHP
PHP排序算法之希尔排序(Shell Sort)实例分析
2018/04/20 PHP
PHP后期静态绑定之self::限制实例分析
2018/12/21 PHP
MSN消息提示类
2006/09/05 Javascript
JS getAttribute和setAttribute(取得和设置属性)的使用介绍
2013/07/10 Javascript
JS如何将数字类型转化为没3个一个逗号的金钱格式
2014/01/27 Javascript
轻松学习jQuery插件EasyUI EasyUI创建菜单与按钮
2015/11/30 Javascript
JS实现单击输入框弹出选择框效果完整实例
2015/12/14 Javascript
JQuery点击行tr实现checkBox选中的简单实例
2016/05/26 Javascript
JavaScript sort数组排序方法和自我实现排序方法小结
2016/06/06 Javascript
AngularJS解决ng-if中的ng-model值无效的问题
2017/06/21 Javascript
浅谈sass在vue注意的地方
2017/08/10 Javascript
bootstrap-paginator服务器端分页使用方法详解
2020/02/13 Javascript
Vue时间轴 vue-light-timeline的用法说明
2020/10/29 Javascript
python中元类用法实例
2014/10/10 Python
Python 的 Socket 编程
2015/03/24 Python
在Python中处理日期和时间的基本知识点整理汇总
2015/05/22 Python
Python实现处理管道的方法
2015/06/04 Python
Python实现删除文件但保留指定文件
2015/06/21 Python
使用Mixin设计模式进行Python编程的方法讲解
2016/06/21 Python
利用Python将时间或时间间隔转为ISO 8601格式方法示例
2017/09/05 Python
python3使用SMTP发送简单文本邮件
2018/06/19 Python
jupyter 使用Pillow包显示图像时inline显示方式
2020/04/24 Python
python+selenium自动化实战携带cookies模拟登陆微博
2021/01/19 Python
详解css3 mask遮罩实现一些特效
2018/10/24 HTML / CSS
CSS3网格的三个新特性详解
2014/04/04 HTML / CSS
CSS3实现文字波浪线效果示例代码
2016/11/20 HTML / CSS
捷克街头、运动和滑板一站式商店:BoardStar.cz
2019/10/06 全球购物
Whistles官网:英国女装品牌
2020/08/14 全球购物
static全局变量与普通的全局变量有什么区别
2014/05/27 面试题
竞选体育委员演讲稿
2014/04/26 职场文书
幼儿教师师德承诺书
2014/05/23 职场文书
2014年感恩母亲演讲稿
2014/05/27 职场文书
2015年信息宣传工作总结
2015/05/26 职场文书
创业计划书之儿童理发店
2019/09/27 职场文书
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2021/06/21 Java/Android