This article mainly introduces the export MONGO library to the local, has a certain reference value, now share to everyone, the need for friends can refer to
Demand:
Under the YII framework, export the data from the production MONGO library to the JSON file and download to the local
Call:
1. Quoting under/web/controllers/testcontroller.php
Public Function Actionexport () {public $target = '/www/web/html/import/'; The directory where the Windows export files are located $export =new export ($target, ' Questionuser ', "); }
2. Create a export.php under/web/model
<?php/* Export data in MONGO library * @author Lizhihui * @date 2018-5-29* Invocation Example: Export (' MyModel ', 96); Export Data */class export{public $target qid 96 in database MyModel; Public $model; Database object string, for example: ' QuestionAnswer ', ' questionuser ' public $qId; Questionnaire ID int public $db; /* * $target the directory where the exported file is located * $model * $qId */Public function __construct ($target, $model, $qId) { $this->target = $target; $this->db= $model; $this->model = new $model; $this->qid = (int) $qId; $this->export (); }/** * Export MONGO production data for local testing * @author Lizhihui * @date 2018-5-29 */Public Function export () { $iCount = $this->model->count (' Conditions ' =>array (' qId ' =>array (' Equals ' => ; $this->qid))); if (! $iCount) {$this->showmessage (' data is null '); } $nStart = 0; Start record $nCount = 100; Number of records processed per $nPage = Intval ($iCount/$nCount) +1; $aReault =array (); for ($i =0; $i < $nPage;) {$sWhere = array (' Conditions ' =>array (' qId ' =>array (' equals ' = = $qI d), ' limit ' = $nCount, ' offset ' = $nStart,); $arr = $this->model->findall ($sWhere); if (!is_dir ($this->target)) {mkdir ($this->target); }//write to file $limit = 1000;//every $limit line, refresh the output buffer, not too large, nor too small foreach ($arr as $key = = $v AL) {if ($key!=0 && $key% $limit ==0) {Ob_flush (); Flush (); } $attr = $val->attributes; Organize data, delete unnecessary key value 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)); Download file if (!file_exists ($filePath)) {$this->showmessage (' destination file does not exist! '); } header (' Content-type:application/json '); Header (' content-disposition:attachment; filename= '. $this->db. ' _ '. $this->qid. JSON '); Header (' accept-ranges:bytes '); Echo file_get_contents ($filePath); }/** * Information output */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 whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!