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 相关文章推荐
Zend Guard一些常见问题解答
Sep 11 PHP
php SQL之where语句生成器
Mar 24 PHP
AMFPHP php远程调用(RPC, Remote Procedure Call)工具 快速入门教程
May 10 PHP
php循环检测目录是否存在并创建(循环创建目录)
Jan 06 PHP
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
May 15 PHP
php匹配字符中链接地址的方法
Dec 22 PHP
php实现的验证码文件类实例
Jun 18 PHP
PHP7之Mongodb API使用详解
Dec 26 PHP
php结合web uploader插件实现分片上传文件
May 10 PHP
Yii2实现同时搜索多个字段的方法
Aug 10 PHP
phpcms配置列表页以及获得文章发布时间
Jul 04 PHP
php从数据库中获取数据用ajax传送到前台的方法
Aug 20 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
杏林同学录(六)
2006/10/09 PHP
php中引用符号(&amp;)的使用详解
2013/11/13 PHP
PHP函数checkdnsrr用法详解(Windows平台用法)
2016/03/21 PHP
laravel框架邮箱认证实现方法详解
2019/11/22 PHP
PHP CURL实现模拟登陆并上传文件操作示例
2020/01/02 PHP
input 输入框内的输入事件详细分析
2010/03/17 Javascript
解决Jquery鼠标经过不停滑动的问题
2014/03/03 Javascript
浅析JS中document对象的一些重要属性
2014/03/06 Javascript
jQuery使用之标记元素属性用法实例
2015/01/19 Javascript
基于JS实现的倒计时程序实例
2015/07/24 Javascript
easyui Droppable组件实现放置特效
2015/08/19 Javascript
jQuery实现带有上下控制按钮的简单多行滚屏效果代码
2015/09/04 Javascript
jquery实现初次打开有动画效果的网页TAB切换代码
2015/09/06 Javascript
url传递的参数值中包含&amp;时,url自动截断问题的解决方法
2016/08/02 Javascript
vue 1.x 交互实现仿百度下拉列表示例
2017/10/21 Javascript
详解小程序缓存插件(mrc)
2018/08/17 Javascript
JS中自定义事件的使用与触发操作实例分析
2019/11/01 Javascript
Vue 中获取当前时间并实时刷新的实现代码
2020/05/12 Javascript
Python获取DLL和EXE文件版本号的方法
2015/03/10 Python
python实现带错误处理功能的远程文件读取方法
2015/04/29 Python
Python浅拷贝与深拷贝用法实例
2015/05/09 Python
Python字符串替换实例分析
2015/05/11 Python
浅谈python中的面向对象和类的基本语法
2016/06/13 Python
Django管理员账号和密码忘记的完美解决方法
2018/12/06 Python
Python实现的对本地host127.0.0.1主机进行扫描端口功能示例
2019/02/15 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
2020/07/07 Python
python打开音乐文件的实例方法
2020/07/21 Python
pytorch 移动端部署之helloworld的使用
2020/10/30 Python
python cookie反爬处理的实现
2020/11/01 Python
国际知名设计师时装商店:Coggles
2016/09/05 全球购物
美体小铺瑞典官方网站:The Body Shop瑞典
2018/01/27 全球购物
飞利浦美国官网:Philips美国
2020/02/28 全球购物
历史系自荐信范文
2013/12/24 职场文书
心得体会范文
2014/01/04 职场文书
会计自我鉴定
2014/02/04 职场文书
家具公司总经理岗位职责
2014/07/08 职场文书