about how to export the MONGO library to a local problem resolution

Source: Internet
Author: User
Tags echo date
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 ' =&gt ;        $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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.