Code Monkey home page Code Monkey logo

mongoimport's Introduction

mongoimport

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.