mongoexport 导出的实例

参考文档    

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;


      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值