public function csvCommand($services = [], $collection_pattern = '', $bom = false)
    {
        foreach ($this->_getServices($services) as $service) {
            /**
             * @var \ManaPHP\Mongodb $mongodb
             */
            $mongodb = $this->_di->getShared($service);
            $defaultDb = $mongodb->getDefaultDb();

            foreach ($defaultDb ? [$defaultDb] : $mongodb->listDatabases() as $db) {
                if (in_array($db, ['admin', 'local'], true)) {
                    continue;
                }

                foreach ($mongodb->listCollections($db) as $collection) {
                    if ($collection_pattern && !fnmatch($collection_pattern, $collection)) {
                        continue;
                    }

                    $fileName = "@tmp/mongodb_csv/$db/$collection.csv";

                    $this->console->progress(['`:collection` processing...', 'collection' => $collection], '');

                    $this->filesystem->dirCreate(dirname($fileName));

                    $file = fopen($this->alias->resolve($fileName), 'wb');

                    if ($bom) {
                        fprintf($file, "\xEF\xBB\xBF");
                    }

                    $docs = $mongodb->fetchAll("$db.$collection");

                    if ($docs) {
                        $columns = [];
                        foreach ($docs[0] as $k => $v) {
                            if ($k === '_id' && is_object($v)) {
                                continue;
                            }
                            $columns[] = $k;
                        }

                        fputcsv($file, $columns);
                    }

                    $linesCount = 0;
                    $startTime = microtime(true);
                    if (count($docs) !== 0) {
                        foreach ($docs as $doc) {
                            $line = [];
                            foreach ($doc as $k => $v) {
                                if ($k === '_id' && is_object($v)) {
                                    continue;
                                }
                                $line[] = $v;
                            }

                            $linesCount++;
                            fputcsv($file, $line);
                        }
                    }

                    fclose($file);

                    $this->console->progress(['write to `:file` success: :count [:time]',
                        'file' => $fileName,
                        'count' => $linesCount,
                        'time' => round(microtime(true) - $startTime, 4)]);
                    /** @noinspection DisconnectedForeachInstructionInspection */
                }
            }

        }
    }
public function csvCommand($services = [], $collection_pattern = '', $bom = false)
    {
        foreach ($this->_getServices($services) as $service) {
            /**
             * @var \ManaPHP\Mongodb $mongodb
             */
            $mongodb = $this->_di->getShared($service);
            $defaultDb = $mongodb->getDefaultDb();

            foreach ($defaultDb ? [$defaultDb] : $mongodb->listDatabases() as $db) {
                if (in_array($db, ['admin', 'local'], true)) {
                    continue;
                }

                foreach ($mongodb->listCollections($db) as $collection) {
                    if ($collection_pattern && !fnmatch($collection_pattern, $collection)) {
                        continue;
                    }

                    $fileName = "@tmp/mongodb_csv/$db/$collection.csv";

                    $this->console->progress(['`:collection` processing...', 'collection' => $collection], '');

                    $this->filesystem->dirCreate(dirname($fileName));

                    $file = fopen($this->alias->resolve($fileName), 'wb');

                    if ($bom) {
                        fprintf($file, "\xEF\xBB\xBF");
                    }

                    $docs = $mongodb->fetchAll("$db.$collection");

                    if ($docs) {
                        $columns = [];
                        foreach ($docs[0] as $k => $v) {
                            if ($k === '_id' && is_object($v)) {
                                continue;
                            }
                            $columns[] = $k;
                        }

                        fputcsv($file, $columns);
                    }

                    $linesCount = 0;
                    $startTime = microtime(true);
                    if (count($docs) !== 0) {
                        foreach ($docs as $doc) {
                            $line = [];
                            foreach ($doc as $k => $v) {
                                if ($k === '_id' && is_object($v)) {
                                    continue;
                                }
                                $line[] = $v;
                            }

                            $linesCount++;
                            fputcsv($file, $line);
                        }
                    }

                    fclose($file);

                    $this->console->progress(['write to `:file` success: :count [:time]',
                        'file' => $fileName,
                        'count' => $linesCount,
                        'time' => round(microtime(true) - $startTime, 4)]);
                    /** @noinspection DisconnectedForeachInstructionInspection */
                }
            }

        }
    }
public function csvCommand ( $ services = [ ] , $ collection_pattern = '' , $ bom = false ) { foreach ( $ this -> _getServices ( $ services ) as $ service ) { /**
             * @var \ManaPHP\Mongodb $mongodb
             */ $ mongodb = $ this -> _di -> getShared ( $ service ) ; $ defaultDb = $ mongodb -> getDefaultDb ( ) ; foreach ( $ defaultDb ? [ $ defaultDb ] : $ mongodb -> listDatabases ( ) as $ db ) { if ( in_array ( $ db , [ 'admin' , 'local' ] , true ) ) { continue ; } foreach ( $ mongodb -> listCollections ( $ db ) as $ collection ) { if ( $ collection_pattern && ! fnmatch ( $ collection_pattern , $ collection ) ) { continue ; } $ fileName = "@tmp/mongodb_csv/$db/$collection.csv" ; $ this -> console -> progress ( [ '`:collection` processing...' , 'collection' => $ collection ] , '' ) ; $ this -> filesystem -> dirCreate ( dirname ( $ fileName ) ) ; $ file = fopen ( $ this -> alias -> resolve ( $ fileName ) , 'wb' ) ; if ( $ bom ) { fprintf ( $ file , "\xEF\xBB\xBF" ) ; } $ docs = $ mongodb -> fetchAll ( "$db.$collection" ) ; if ( $ docs ) { $ columns = [ ] ; foreach ( $ docs [ 0 ] as $ k => $ v ) { if ( $ k === '_id' && is_object ( $ v ) ) { continue ; } $ columns [ ] = $ k ; } fputcsv ( $ file , $ columns ) ; } $ linesCount = 0 ; $ startTime = microtime ( true ) ; if ( count ( $ docs ) !== 0 ) { foreach ( $ docs as $ doc ) { $ line = [ ] ; foreach ( $ doc as $ k => $ v ) { if ( $ k === '_id' && is_object ( $ v ) ) { continue ; } $ line [ ] = $ v ; } $ linesCount ++ ; fputcsv ( $ file , $ line ) ; } } fclose ( $ file ) ; $ this -> console -> progress ( [ 'write to `:file` success: :count [:time]' , 'file' => $ fileName , 'count' => $ linesCount , 'time' => round ( microtime ( true ) - $ startTime , 4 ) ] ) ; /** @noinspection DisconnectedForeachInstructionInspection */ } } } }
export mongodb data to csv files

@param array  $services services list
@param string $collection_pattern match collection against a pattern
@param bool   $bom contains BOM or not
export mongodb data to csv files