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
967456
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 */
                }
            }

        }
    }
1142300
public function prepare(string $confKey, string $db, string $collection)
    {
        $this->profileName = 'mongo.' . $db . '.';
        $this->config = getInstance()->config['mongodb'] ?? [];
        if (!isset($this->config[$confKey])) {
            throw new \MongoConnectionException('No such a MongoDB config ' . $confKey);
        }
        $conf = $this->config[$confKey];
        $this->mongoClient = new \MongoClient($conf['server'], $conf['options'], $conf['driverOptions']);
        $db && ($this->mongoDb = $this->mongoClient->selectDB($db));
        $collection && ($this->mongoCollection = $this->mongoDb->selectCollection($collection));
    }
424389
public static function remapCollection(array $collection, array $map, $createMode = true)
    {
        $results = array();

        foreach ($collection as $item) {
            $results[] = self::remap($item, $map, $createMode);
        }

        return $results;
    }
1297093
public function createCollectionTranslation(Collection $collection, string $locale): void
    {
        $query = $this->connection->createQueryBuilder()
            ->insert('ngbm_collection_translation')
            ->values(
                [
                    'collection_id' => ':collection_id',
                    'status' => ':status',
                    'locale' => ':locale',
                ]
            )
            ->setParameter('collection_id', $collection->id, Type::INTEGER)
            ->setParameter('status', $collection->status, Type::INTEGER)
            ->setParameter('locale', $locale, Type::STRING);

        $query->execute();
    }
10493
protected function format(array $services): array
    {
        return Collection::make($services)->map(function (array $service) {
            return [
                'id'   => $service['ID'],
                'name' => $service['Service'],
                'host' => $service['Address'],
                'port' => $service['Port'],
            ];
        })->groupBy('name')->toArray();
    }
804743
public function match(array $models, Collection $results, $relation)
    {
        $foreign = $this->foreignKey;

        $other = $this->otherKey;

        $dictionary = array();

        foreach ($results as $result) {
            $dictionary[$result->getAttribute($other)] = $result;
        }

        foreach ($models as $model) {
            if (isset($dictionary[$model->$foreign])) {
                $model->setRelation($relation, $dictionary[$model->$foreign]);
            }
        }

        return $models;
    }
961053
protected function denormalizeCollection($collection, $class, $name, $format = null, array $context = array())
    {
        /** @var ClassMetadata $meta */
        $meta = $context['entityMeta'];

        $targetEntity = $meta->associationMappings[$name]['targetEntity'];

        $result = array();
        foreach ($collection as $key => $element) {
            $class = $class ?: $targetEntity;

            $result[$key] = $this->denormalizeAssociation($element, $class, $name, $format, $context);
        }

        return $result;
    }
606519
protected function rest_list($from = null, $limit = null, array $filters)
    {
        $filters = array_merge(["from" => $from, "limit" => $limit], $filters);

        $apiPath = $this->getResourcePath() . $this->buildQueryString($filters);

        $result = $this->client->performHttpCall(self::REST_LIST, $apiPath);

        /** @var BaseCollection $collection */
        $collection = $this->getResourceCollectionObject($result->count, $result->_links);

        foreach ($result->_embedded->{$collection->getCollectionResourceName()} as $dataResult) {
            $collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
        }

        return $collection;
    }
891331
protected function _getServices($services)
    {
        if ($services) {
            foreach ($services as $index => $service) {
                if (!$this->_di->has($service)) {
                    if ($this->_di->has($service . 'Mongodb')) {
                        $services[$index] = $service . 'Mongodb';
                    } elseif ($this->_di->has($service . '_mongodb')) {
                        $services[$index] = $service . '_mongodb';
                    } else {
                        $this->console->warn(['`:service` service is not exists: ignoring', 'service' => $service]);
                        unset($services[$index]);
                    }
                }
            }
        } else {
            $services = [];
            foreach ($this->configure->components as $service => $config) {
                $config = json_encode($config, JSON_UNESCAPED_SLASHES);
                if (preg_match('#mongodb://#', $config)) {
                    $services[] = $service;
                }
            }
        }

        return $services;
    }
1275824
public function serve(string $collection, string $group, ?string $format = null) : string {
        if ( ! isset($this->app['basset'][$collection])) {
            return '<!-- Basset could not find collection: '.$collection.' -->';
        }

        // Get the collection instance from the array of collections. This instance will be used
        // throughout the building process to fetch assets and compare against the stored
        // manfiest of fingerprints.
        $collection = $this->app['basset'][$collection];

        if ($this->runningInProduction() and $this->app['basset.manifest']->has($collection)) {
            if ($this->app['basset.manifest']->get($collection)->hasProductionFingerprint($group)) {
                return $this->serveProductionCollection($collection, $group, $format);
            }
        }

        return $this->serveDevelopmentCollection($collection, $group, $format);
    }
794524
public function serializeCollection($collection, TransformerAbstract $transformer = null, $resourceKey = null)
    {
        return $this->getFractalService()
            ->collection($collection, $transformer, $resourceKey);
    }
483220
@Override
    public List<ExportFormat> exportFormats(IssueServiceConfiguration issueServiceConfiguration) {
        return issueExportServiceFactory.getIssueExportServices().stream()
                .map(IssueExportService::getExportFormat)
                .collect(Collectors.toList());
    }
1295829
protected function serializeEmbeddedCollection(MapperInterface $mapper, array $collection)
    {
        $data = array();
        foreach ($collection as $object) {
            $data[] = $mapper->serialize($object);
        }

        return $data;
    }
814882
public function build($collection, array $fields, $config = [])
    {
        $this->config = $config;
        $csv = $this->writer;
        $headers = [];

        foreach ($fields as $key => $field) {
            $headers[] = $field;

            if (!is_numeric($key)) {
                $fields[$key] = $key;
            }
        }

        if (is_a($collection, EloquentCollection::class)) {
            $this->isEloquentCollection = true;
        }

        $this->addHeader($csv, $headers);
        $this->addCsvRows($collection, $fields, $csv);

        return $this;
    }
1372426
public function match(array $models, Collection $results, $relation)
    {
        // We will need the parent node placeholder so that we use it to extract related results.
        $parent = $this->query->getQuery()->modelAsNode($this->parent->getTable());

        /**
         * Looping into all the parents to match back onto their children using
         * the primary key to map them onto the correct instances, every single
         * result will be having both instances at each Collection item, held by their
         * node placeholder.
         */
        foreach ($models as $model)
        {
            $matched = $results->filter(function($result) use($parent, $model)
            {
                if ($result[$parent] instanceof Model)
                {
                    // In the case of fetching nested relations, we will get an array
                    // with the first key being the model we need, and the other being
                    // the related model so we'll just take the first model out of the array.
                    if (is_array($model)) $model = reset($model);

                    return $model->getKey() == $result[$parent]->getKey();
                }
            });

            // Now that we have the matched parents we know where to add the relations.
            // Sometimes we have more than a match so we gotta catch them all!
            foreach ($matched as $match)
            {
                // In the case of fetching nested relations, we will get an array
                // with the first key being the model we need, and the other being
                // the related model so we'll just take the first model out of the array.
                if (is_array($model)) $model = reset($model);

                $model->setRelation($relation, $match[$relation]);
            }
        }

        return $models;
    }
227969
public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = [])
    {
        if (!$this->pagination->isEnabled($resourceClass, $operationName, $context)) {
            return;
        }

        $context = $this->addCountToContext(clone $aggregationBuilder, $context);

        [, $offset, $limit] = $this->pagination->getPagination($resourceClass, $operationName, $context);

        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
        if (!$manager instanceof DocumentManager) {
            throw new RuntimeException(sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class));
        }

        $repository = $manager->getRepository($resourceClass);
        if (!$repository instanceof DocumentRepository) {
            throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class));
        }

        $resultsAggregationBuilder = $repository->createAggregationBuilder()->skip($offset);
        if ($limit > 0) {
            $resultsAggregationBuilder->limit($limit);
        } else {
            // Results have to be 0 but MongoDB does not support a limit equal to 0.
            $resultsAggregationBuilder->match()->field(Paginator::LIMIT_ZERO_MARKER_FIELD)->equals(Paginator::LIMIT_ZERO_MARKER);
        }

        $aggregationBuilder
            ->facet()
            ->field('results')->pipeline(
                $resultsAggregationBuilder
            )
            ->field('count')->pipeline(
                $repository->createAggregationBuilder()
                    ->count('count')
            );
    }
711064
public function match(array $models, Collection $results, $relation)
    {
        // This relationship deals with a One-To-One morph type so we'll just extract
        // the first model out of the results and return it.
        $matched = parent::match($models, $results, $relation);

        return array_map(function($match) use ($relation)
        {
            if (isset($match[$relation]) && isset($match[$relation][0]))
            {
                $match->setRelation($relation, $match[$relation][0]);
            }

            return $match;

        }, $matched);
    }