So I created a web app on cloud9 using Node.js, mongoDB, express. I set up MongoDB in cloud 9 as per the instructions.
When I completed the app, I deployed it to Heroku, and created a new db in mlab (mongolab). This works fine.
However, I inserted a lot of dummy data into the db hosted on cloud9. I want to copy this dummy data from the db in cloud9 to the db in mlab. How do i do that?
I did some research.
According to mongodb docs, I should use db.copyDatabase():
db.copydatabase(fromdb,todb,fromhost,username,password,mechanism)
It says I should run this command in the destination instance, i.e. the instance receiving the copied data.
I'm assuming this means that I should connect to the db hosted on mlab through: mongo dsblahblah.mlab.com:59220/blahblah -u -p
and then run the db.copyDatabase command, correct?
But what should I use for the fromdb parameter? This is the db that is hosted in cloud9. But what is this db's url?
Help. And ELI5.
Nathan Loyer has the correct answer, though it's of course more complicated than that. If you're like me and don't really know what you're doing, here's a detailed explanation that worked for me, with resources for where I got this information at bottom in case anything gets updated and makes you out of date.
Note that you need to do this for each collection you want to migrate. Chances are you only have one or a few collections, so that's not much hassle. If you want to move a full db, it's possible these instructions can be adapted, but that's up to you.
mongo
show dbs
use (the name of the appropriate db, not in parentheses though)
show collections
mongoexport --db (your db name) --collection (your collection name) --outs (a filename: mongodb.org recommends "traffic.json", no quotes)
mongoimport -h ds12345.mlab.com:12121 -d databasename -c <collection> -u <user> -p <password> --file <input file, possibly traffic.json?>
The other thing you need to do is stop all processes in Cloud9, including Mongod, which operates from the root directory. This is where you need to run mongoimport from (NOT from your command line inside some directory in your workspace, which is where I told you to run mongoexport from). To be clear: Your command line should show "username: ~$", not "username:~/workspace/filedirectories/maybeafewofthem (possibly master) $".
Since you're running from the root, you also need to make sure the filename you're passing is referenced correctly. It won't be as simple as "traffic.json." It should look something like "workspace/directoryName/traffic.json." (If your export was successful, you should see the traffic.json file floating around your files somewhere.)
And that's it. Run the code from Step 3, and check in mLabs that it processed correctly. Good to go.
Resources:
http://docs.mlab.com/connecting/
http://docs.mlab.com/migrating/
https://docs.mongodb.com/manual/reference/program/mongoexport/
https://docs.mongodb.com/manual/reference/program/mongoimport/