Setup Docker Mongo and Import CSV Data

Setting up docker MongoDB and importing CSV is easy. Just follow these steps.

How to Setup Docker Mongo

  1. I’m assuming you’ve already got Docker installed on your system. if you don’t, do that first.
  2. For the following steps, do not actually run the command until the instruction says so. This is going through the building of the command for your own information.
  3. to get docker mongo from the MongoDB main docker repository, refer to the following:
    docker run --name mongo -d mongo
  4. We will add exposure to both the mongodb connector plugin port, 27017, and the web interface port, 28017, which requires the –httpinterface flag.
    docker run --name mongo -p 27017:27017 -p 28017:28017 --httpinterface
  5. Now we add a mount to a local volume, so you can have local access to the data files created by mongo. Also, just to be nice to the filesystem, we’ll enable smallfiles mode.
    docker run --name mongo -p 27017:27017 -p 28017:28017 -v /my/own/datadir:/data/db -d mongo --httpinterface --smallfiles
  6. Now, also, lets mount the directory on your local system where your CSV data file lives:
    docker run --name mongo -p 27017:27017 -p 28017:28017 -v /my/own/datadir:/data/db -v /path/to/sample/data/file:/data/seeds -d mongo --httpinterface --smallfiles
  7. Finally, hit enter, and run the command from step 6

Go inside the running Docker Mongo instance so you can import data

sudo docker exec -i -t mongo bash

Now, you’re inside the Docker instance and you have a command line waiting for input.

Run in the import of your CSV Data

mongoimport --db someDatabaseName --collection someCollectionName --type csv --headerline --file /data/seeds/yourfile.csv

Note: the –db name and the –collection name provided doesn’t need to already exist. They will be automatically created upon execution of the import command