参考文档
mongoexport https://docs.mongodb.com/manual/reference/program/mongoexport/index.html
<?php function checkDir($dir_name) { if (!file_exists($dir_name) || !is_dir($dir_name)) { @mkdir($dir_name, 0777, true); } } // check dir checkDir('mongo_collect1'); checkDir('mongo_collect2'); checkDir('mongo_collect3'); checkDir('mongo_collect4'); // dump collect1 $config = [ 'tid' => 'you tid', 'host' => 'your host', 'limit' => 2000 ]; $dump_collect1 =<<<EOF mongoexport --host {$config['host']} -d crs -c collect1 -q '{"status":0, "tid":"{$config['tid']}"}' --sort '{_id: -1}' --limit {$config['limit']} --out mongo_collect1/collect1.json EOF; // exec exec($dump_collect1); // dump data $file_name = 'mongo_collect1/collect1.json'; $collect1_json = file_get_contents($file_name); // explode by PHP_EOL $collect1_arr = explode(PHP_EOL, $collect1_json); echo 'begin 导出数据' . PHP_EOL; $user_tel_list = []; foreach ($collect1_arr as $sid_json) { // get sid tel $sid_arr = json_decode($sid_json, true); $sid = $sid_arr['sid']; $tel = $sid_arr['tel']; // export collect2 $dump_collect2 =<<<EOF mongoexport --host {$config['host']} -d crs -c collect2 -q '{"sid":"$sid"}' --out mongo_collect2/$sid.json EOF; exec($dump_collect2); // export collect3 $dump_collect3 =<<<EOF mongoexport --host {$config['host']} -d crs -c collect3 -q '{"sid":"$sid"}' --out mongo_collect3/$sid.json EOF; exec($dump_collect3); // export collect4 if (in_array($tel, $user_tel_list)) { continue; } $user_tel_list[] = $tel; $dump_collect4 =<<<EOF mongoexport --host {$config['host']} -d crs -c collect4 -q '{"tel":"$tel"}' --out mongo_collect4/$tel.json EOF; exec($dump_collect4); } echo 'end 导出数据' . PHP_EOL;