Search code examples
node.jsmongodbherokucloud9-idemlab

How to export MongoDB database from cloud9 to mlab?


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.


Solution

  • 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.

    1. Figure out which collection you want to move. To do that, in Cloud9, at the appropriate directory, in your command line, run the following commands, and remember or write down the db and collection you identify. (If you're not sure on the collections, you can do db.(collection).find() to output a list of data it has)
    mongo
    show dbs
    use (the name of the appropriate db, not in parentheses though)
    show collections
    
    1. Export the data from your database. That's relatively easy. Simply exit mongo with ctrl+c, and use the following code in your command line, with data filled in and no parentheses:
    mongoexport --db (your db name) --collection (your collection name) --outs (a filename: mongodb.org recommends "traffic.json", no quotes)
    
    1. Now you need to import your data into mLabs. Go to mLabs, select your database, and pick the "tools" menu. You should see a pre-fab code under "JSON" for importing. Fill in the blanks again, using your database user credentials from mLab, but don't run it yet. It should look something like:
    mongoimport -h ds12345.mlab.com:12121 -d databasename -c <collection> -u <user> -p <password> --file <input file, possibly traffic.json?>
    
    1. In order to pass the import successfully, you should check that your Cloud9 and mLabs instances are running on the same (or at least very similar versions). In cloud9, just type "mongo" into the console and see what it says. You're probably on 2.6 or 3.2. On mLab, it's on almost every page. Just look for "mongod version: xxxxx." You're probably on 3.4.7. I ran it as Cloud9 3.2 with mLab 3.4, and it worked fine. (If your Cloud9 is on less than version 3, backup anything important (use git?) and update per the instructions at https://community.c9.io/t/updating-mongodb/3914 )

    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/