首頁  >  文章  >  後端開發  >  關於如何匯出mongo庫到本地的問題解決

關於如何匯出mongo庫到本地的問題解決

不言
不言原創
2018-07-10 15:24:521259瀏覽

這篇文章主要介紹了關於導出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

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn