devmachine/mongoimport

This package is abandoned and no longer maintained. No replacement package was suggested.

Import MongoDB export into Doctrine.

1.0.4 2015-12-23 08:20 UTC

This package is not auto-updated.

Last update: 2023-02-24 00:14:50 UTC


README

Build Status Coverage Status

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