public function listCommand($services = [], $collection_pattern = '', $field = '', $db = [])
    {
        foreach ($this->_getServices($services) as $service) {
            /**
             * @var \ManaPHP\Mongodb $mongodb
             */
            $mongodb = $this->_di->getShared($service);

            $defaultDb = $mongodb->getDefaultDb();
            foreach ($defaultDb ? [$defaultDb] : $mongodb->listDatabases() as $cdb) {
                if ($db && !in_array($cdb, $db, true)) {
                    continue;
                }

                $this->console->writeLn(['---`:db` db of `:service` service---', 'db' => $cdb, 'service' => $service], Console::BC_CYAN);
                foreach ($mongodb->listCollections($cdb) as $row => $collection) {
                    if ($collection_pattern && !fnmatch($collection_pattern, $collection)) {
                        continue;
                    }
                    if ($field) {
                        if (!$docs = $mongodb->fetchAll("$cdb.$collection", [$field => ['$exists' => 1]], ['limit' => 1])) {
                            continue;
                        }
                    } else {
                        $docs = $mongodb->fetchAll("$cdb.$collection", [], ['limit' => 1]);
                    }
                    $columns = $docs ? array_keys($docs[0]) : [];

                    $this->console->writeLn([' :row :namespace(:columns)',
                        'row' => sprintf('%2d ', $row + 1),
                        'namespace' => $this->console->colorize("$cdb.$collection", Console::FC_GREEN),
                        'columns' => implode(', ', $columns)]);
                }
            }
        }
    }
public function listCommand($services = [], $collection_pattern = '', $field = '', $db = [])
    {
        foreach ($this->_getServices($services) as $service) {
            /**
             * @var \ManaPHP\Mongodb $mongodb
             */
            $mongodb = $this->_di->getShared($service);

            $defaultDb = $mongodb->getDefaultDb();
            foreach ($defaultDb ? [$defaultDb] : $mongodb->listDatabases() as $cdb) {
                if ($db && !in_array($cdb, $db, true)) {
                    continue;
                }

                $this->console->writeLn(['---`:db` db of `:service` service---', 'db' => $cdb, 'service' => $service], Console::BC_CYAN);
                foreach ($mongodb->listCollections($cdb) as $row => $collection) {
                    if ($collection_pattern && !fnmatch($collection_pattern, $collection)) {
                        continue;
                    }
                    if ($field) {
                        if (!$docs = $mongodb->fetchAll("$cdb.$collection", [$field => ['$exists' => 1]], ['limit' => 1])) {
                            continue;
                        }
                    } else {
                        $docs = $mongodb->fetchAll("$cdb.$collection", [], ['limit' => 1]);
                    }
                    $columns = $docs ? array_keys($docs[0]) : [];

                    $this->console->writeLn([' :row :namespace(:columns)',
                        'row' => sprintf('%2d ', $row + 1),
                        'namespace' => $this->console->colorize("$cdb.$collection", Console::FC_GREEN),
                        'columns' => implode(', ', $columns)]);
                }
            }
        }
    }
public function listCommand ( $ services = [ ] , $ collection_pattern = '' , $ field = '' , $ db = [ ] ) { foreach ( $ this -> _getServices ( $ services ) as $ service ) { /**
             * @var \ManaPHP\Mongodb $mongodb
             */ $ mongodb = $ this -> _di -> getShared ( $ service ) ; $ defaultDb = $ mongodb -> getDefaultDb ( ) ; foreach ( $ defaultDb ? [ $ defaultDb ] : $ mongodb -> listDatabases ( ) as $ cdb ) { if ( $ db && ! in_array ( $ cdb , $ db , true ) ) { continue ; } $ this -> console -> writeLn ( [ '---`:db` db of `:service` service---' , 'db' => $ cdb , 'service' => $ service ] , Console :: BC_CYAN ) ; foreach ( $ mongodb -> listCollections ( $ cdb ) as $ row => $ collection ) { if ( $ collection_pattern && ! fnmatch ( $ collection_pattern , $ collection ) ) { continue ; } if ( $ field ) { if ( ! $ docs = $ mongodb -> fetchAll ( "$cdb.$collection" , [ $ field => [ '$exists' => 1 ] ] , [ 'limit' => 1 ] ) ) { continue ; } } else { $ docs = $ mongodb -> fetchAll ( "$cdb.$collection" , [ ] , [ 'limit' => 1 ] ) ; } $ columns = $ docs ? array_keys ( $ docs [ 0 ] ) : [ ] ; $ this -> console -> writeLn ( [ ' :row :namespace(:columns)' , 'row' => sprintf ( '%2d ' , $ row + 1 ) , 'namespace' => $ this -> console -> colorize ( "$cdb.$collection" , Console :: FC_GREEN ) , 'columns' => implode ( ', ' , $ columns ) ] ) ; } } } }
list databases and collections

@param array  $services services list
@param string $collection_pattern match collection against a pattern
@param string $field collection must contains one this field
@param array  $db
list databases and collections