日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php导出mongo日志,导出mongo库到本地

發(fā)布時(shí)間:2025/3/20 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php导出mongo日志,导出mongo库到本地 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

需求:

在yii框架架下,導(dǎo)出生產(chǎn)mongo庫(kù)中的數(shù)據(jù)到j(luò)son文件,下載到本地

調(diào)用:

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

public function actionExport()

{

public $target='/WWW/web/html/import/'; //windows 導(dǎo)出文件所在目錄

$export =new Export($target,'QuestionUser',96);

}

2.在/web/model下創(chuàng)建Export.php

/*導(dǎo)出mongo庫(kù)中的數(shù)據(jù)

* @author lizhihui

* @date 2018-5-29

* 調(diào)用例子:Export('MyModel',96); //導(dǎo)出數(shù)據(jù)庫(kù)MyModel中qId為96的數(shù)據(jù)

*/

Class Export

{

public $target;

public $model; //數(shù)據(jù)庫(kù)對(duì)象 string,例如:'QuestionAnswer','QuestionUser'

public $qId; //問(wèn)卷id int

public $db;

/*

* $target 導(dǎo)出文件所在目錄

* $model

* $qId

*/

public function __construct($target,$model,$qId)

{

$this->target = $target;

$this->db=$model;

$this->model = new $model;

$this->qId = (int)$qId;

$this->export();

}

/**

* 導(dǎo)出mongo生產(chǎn)數(shù)據(jù)用于本地測(cè)試

* @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('數(shù)據(jù)為空');

}

$nStart = 0; //起始記錄

$nCount = 100; //每次處理記錄數(shù)

$nPage = intval($iCount/$nCount)+1;

$aReault=array();

for ($i=0;$i

$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;

//整理數(shù)據(jù),刪除不必要的鍵值

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('目標(biāo)文件不存在!');

}

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;

}

}

總結(jié)

以上是生活随笔為你收集整理的php导出mongo日志,导出mongo库到本地的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。