18
votes

I'm able to use mongoimport to import csv data into a non-meteor mongodb database, but I can't figure out how to import a csv into my meteor app database.

I learned how to run the mongo shell for my meteor app (meteor mongo) but I can't run mongoimport from the shell.

The mongodb docs for mongoimport says

In this example, mongoimport imports the csv formatted data in the /opt/backups/contacts.csv into the collection contacts in the users database on the MongoDB instance running on the localhost port numbered 27017.

mongoimport --db users --collection contacts --type csv --file /opt/backups/contacts.csv

But when I run mongod, start my meteor app, and run mongoimport it imports to my test database, not my app database.

I read this stackoverflow post comment:

Use mongoexport to dump your collections individually, then mongoimport to import the files into the db named meteor in the meteor mongodb instance. The meteor mongo instance runs on port 3002 with bind_address 127.0.0.1, and the data files are in the meteor project subdirectory .meteor/local/db

But I don't understand how to connect to that instance or how to target it with the mongoimport command.

5

5 Answers

23
votes

Looks like I just answered your comment in Rahuls wonderful answer. Anyway download mongodb from mongodb.org for your OS (or a package manager like macports) and use the tool provided in the bin folder. mongoimport isn't a command in the mongo shell, it's an executable that runs separately.

Also don't forget to put the port in (usually 3001 if you're running your meteor instance at 3000), also the db is usually meteor & not users when you run it

mongoimport -h localhost:3001 --db meteor --collection contacts --type csv --file /opt/backups/contacts.csv
8
votes

NOTE: The above method did NOT work on port 3002 for me, but it DID work on port 3001.

To import a external TSV file into a meteor db, I started the meteor app needing the TSV data. This also launches the meteor mongodb service (in my case on host: localhost:3001), then I opened a terminal in OSX and via the terminal, navigated to a mongodb package bin folder, which I downloaded earlier in order to get the binary, "mongoimport". Once in the bin folder of the mongodb package, then at the command prompt I typed the following below (some switch options will vary...but --host, --localhost and --db switch/values should be as shown):

$  ./mongoimport --host localhost:3001 --db meteor --collection datarefs --type tsv --drop --headerline --file /PathToFile/DataRefs.tsv

After hitting enter, mongoimport echo'd successful import in the terminal. Once this was done, I could navigate to the meteor app via the terminal and launch meteor mongo: $meteor mongo....and see the imported collection, "datarefs" in the meteor db for this app.

$  meteor mongo
...
...
meteor:PRIMARY> show collections
datarefs
system.indexes
meteor:PRIMARY>
4
votes

First create collection in meteor app like this

Students = new Meteor.Collection("students");

Then add some dummy value to be sure meteor initialized collection

Students.insert({"name":"first"}); In some click event for example. To check use this

meteor:PRIMARY> show collections students system.indexes

Then import. mongoimport -h localhost:3001 -d meteor -c students < students.json Somehow meteor doesn't recognize new imported collections you need either remove all of them add some dummy value then import again or create new one and initialize it with dummy value and then import.There might be binding bug because collections show up right in minimongo.

Thank you to Akshat and Tom Kyler

0
votes

A small script (template) for running mongoimport to xxx.meteor.com


    #!/bin/sh

    # Script to import csvfile to meteor application deployed to free meteor.com hosting.
    # Make sure your versions of mongo match with the metor.com mongo versions. 
    # As Jan 2016 it seems to be 3.x something. Tested with mongoimport 3.12.

    if [ $# -eq 0 ]
      then
        echo "usage: $0 xxx.meteor.com collection filename.csv"
        exit 1
    fi

    URL=$1
    COLLECTION=$2
    FILE=$3

    echo Connecting to $URL, please stand by.... collection=$COLLECTION file=$FILE

    PUPMS=`meteor mongo --url $URL | sed 's/mongodb:\/\// -u /' | sed 's/:/ -p /' | sed 's/@/ -h /'  | sed 's/\// -d /'`



    mongoimport -v $PUPMS --type csv --headerline --collection $COLLECTION  --file $FILE

0
votes

Now I use mongochef to move data between databases. It's very easy--you just connect to each database (usually local and remote db) then you can copy and paste documents across collections. Much easier than the command-line approach.