Home  >  Article  >  Backend Development  >  Problem solving on how to export mongo library to local

Problem solving on how to export mongo library to local

不言
不言Original
2018-07-10 15:24:521412browse

This article mainly introduces about exporting mongo library to local, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Requirements:

In yii Under the framework, export the data in the production mongo library to a json file and download it to the local

Call:

1. Quote

 public function actionExport()
    {
        public $target='/WWW/web/html/import/';  //windows 导出文件所在目录
        $export =new Export($target,'QuestionUser',96);
    }
# under /web/Controllers/TestController.php ##2. Create Export.php under /web/model

<?php
/*导出mongo库中的数据
* @author lizhihui 
* @date 2018-5-29
* 调用例子:Export(&#39;MyModel&#39;,96);    //导出数据库MyModel中qId为96的数据
*/
Class Export
{
    public $target;
    public $model;    //数据库对象 string,例如:&#39;QuestionAnswer&#39;,&#39;QuestionUser&#39;
    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(
                &#39;conditions&#39;=>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;
    }
}
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to scan files in a directory and enter them into the database in the Yii framework

The above is the detailed content of Problem solving on how to export mongo library to local. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn