• 技术文章 >后端开发 >php教程

    关于如何导出mongo库到本地的问题解决

    不言不言2018-07-10 15:24:52原创570
    这篇文章主要介绍了关于导出mongo库到本地,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

    需求:

    在yii框架架下,导出生产mongo库中的数据到json文件,下载到本地

    调用:

    1.在/web/Controllers/TestController.php下引用

     public function actionExport()
        {
            public $target='/WWW/web/html/import/';  //windows 导出文件所在目录
            $export =new Export($target,'QuestionUser',96);
        }

    2.在/web/model下创建Export.php

    <?php
    /*导出mongo库中的数据
    * @author lizhihui 
    * @date 2018-5-29
    * 调用例子:Export('MyModel',96);    //导出数据库MyModel中qId为96的数据
    */
    Class Export
    {
        public $target;
        public $model;    //数据库对象 string,例如:'QuestionAnswer','QuestionUser'
        public $qId;    //问卷id int
        public $db;
    
         /*
         * $target 导出文件所在目录
         * $model
         * $qId  
         */
         public function __construct($target,$model,$qId)
         {
             $this->target = $target;
             $this->db=$model;
             $this->model = new $model;
             $this->qId = (int)$qId;
             $this->export();
         }
    
    
    
        /**
        * 导出mongo生产数据用于本地测试
        * @author lizhihui 
        * @date 2018-5-29
        */
        public function Export()
        {
            $iCount = $this->model->count(array(
                'conditions'=>array(
                    'qId'=>array('equals' => $this->qId),
                ))
            );
            if(!$iCount){
                $this->showMessage('数据为空');
            }
    
            $nStart = 0; //起始记录
            $nCount = 100; //每次处理记录数
            $nPage = intval($iCount/$nCount)+1;
            $aReault=array(); 
            for ($i=0;$i<$nPage;) {
                $sWhere = array(
                    'conditions'=>array(
                        'qId'=>array('equals' => $qId),
                    ),
                    'limit'=>$nCount,
                    'offset'=>$nStart,
                );
    
                $arr = $this->model->findAll($sWhere);
                if(!is_dir($this->target)){
                    mkdir($this->target);
                }
                //写入文件
                $limit = 1000;//每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
                foreach ($arr as $key => $val) 
                {
                    if($key!=0 && $key%$limit==0){
                        ob_flush();
                        flush();
                    }
                    $attr=$val->attributes;
                    //整理数据,删除不必要的键值
                    unset($attr['_id']);
                    unset($attr['current_db']);
                    unset($attr['pageInfo']);
                    $aReault[]=$attr;
                }
                $i++;          
                $nStart = $i*$nCount;
            }
            $filePath=$this->target.$this->db.'_'.$this->qId.'.json';
            file_put_contents($filePath,json_encode($aReault));
    
            //下载文件
            if(!file_exists($filePath)){
                $this->showMessage('目标文件不存在!');
            }
            header('Content-Type: application/json');
            header('Content-Disposition: attachment; filename='.$this->db.'_'.$this->qId.'.json');     
            header('Accept-Ranges: bytes');
            echo file_get_contents($filePath);
    
        }
    
    
        /**
         * 信息输出
         */
        private function showMessage($str, $err = 0) {
            if (!$str) {
                return false;
            }
            if ($err) {
                echo "[ERROR]";
            } else {
                echo "[SUCCESS]";
            }
    
            echo date("Y-m-d H:i:s", time()) . " " . $str . "\n";
            exit;
        }
    }

    以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

    相关推荐:

    在yii框架中扫描目录下文件入数据库的方法

    以上就是关于如何导出mongo库到本地的问题解决的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:yii
    上一篇:在yii框架中扫描目录下文件入数据库的方法 下一篇:关于yii改善并发数的性能优化的方法
    大前端线上培训班

    相关文章推荐

    • 分享一份PHP开发内部规范• php session有关问题 • 同一个程序里 二个顶级域名如果共享COOKIE • 网页静态化,几时静 • Jquery应验表单

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网