devmachine / mongoimport
Import MongoDB export into Doctrine.
Installs: 8 807
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
- beberlei/assert: ^2.4
- doctrine/mongodb: ^1.2
Requires (Dev)
This package is not auto-updated.
Last update: 2023-02-24 00:14:50 UTC
README
PHP implementation of mongoimport.
About
Why would you need a custom mongoimport instead of default utility supplied with mongo? In certain setup (read Docker) mongo client is not available. With mongo extension enabled in PHP, you can import JSON created by mongoexport with this tiny library.
Provides integration with Symfony (read below), therefore could be used as fixtures loader.
Installation
Add the following to your composer.json:
{ "require": { "devmachine/mongoimport": "~1.0" } }
Usage
Import movies.json into hollywood database. Collection name is figured out automatically by using file's basename without extension.
$ ./bin/mongoimport movies.json --db hollywood
By default utility connects to mongod running on localhost:27017
. In docker environment default host is MONGO_PORT_27017_TCP_ADDR
and default port is MONGO_PORT_27017_TCP_PORT
.
Overwriting default host, port and collection name:
$ ./bin/mongoimport movies.json -c shows --db hollywood --host <host> -p <port>
To drop existing collection prior to import, use --drop
flag.
More info:
$ ./bin/mongoimport -h
Symfony integration
Register bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(), new Devmachine\MongoImport\Bundle\DevmachineMongoImportBundle(), ); }
When DoctrineMongoDBBundle is enabled it registers the importer in container for each ODM manager e.g.
devmachine_mongoimport.default
(for default manager)devmachine_mongoimport.secondary
(for secondary manager)devmachine_mongoimport
alias for default importer.
Example:
// Import movies.json into "movies" collection in default database. $total = $this ->get('devmachine_mongoimport') ->importCollection('movies.json') ; // Drop existing collection prior to import. $total = $this ->get('devmachine_mongoimport') ->import('movies.json', ['drop' => true]) ; // With specified collection name. $total = $this ->get('devmachine_mongoimport') ->import('movies.json', 'films', ['drop' => true]) ; // With specified collection and db. $total = $this ->get('devmachine_mongoimport') ->import('movies.json', 'films', 'hollywood', ['drop' => true]) ;
Contributing
Find below various docker commands.
Init
$ docker-composer up -d # puts mongo container in bg
Load fixtures
$ docker-compose run --entrypoint php composer fixtures/load.php
Exporting data
From container:
$ mongoexport --db company --collection employees --jsonArray --pretty --out employees.json $ mongoexport --db company --collection offices --jsonArray --pretty --out offices.json
From host:
$ docker cp mongoimport_mongo_1:/employees.json ./tests/fixtures/ $ docker cp mongoimport_mongo_1:/offices.json ./tests/fixtures/
Running tests
$ docker-compose run --entrypoint php composer bin/phpunit
Running utility
$ docker-compose run --entrypoint php composer bin/mongoimport -V