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.
To import data to a MongoDB database, you can use mongoimport to import specific collections data, or you can use mongorestore to import a binary (BSON) full database backup. The exported database file must be stored locally on the same machine as your client.
You can use the --collection (or -c ) parameter to specify a collection to import the file into.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With