Importing Data Into a MongoDB Instance

Whether you’re restoring a backup or migrating a server, importing data is a key step in the process. MongoDB provides many ways to import data into your MongoDB deployment. You can import a collection, database or the whole deployment. In this post, we will talk about three different ways to import data into your ScaleGrid MongoDB hosting deployment.

  1. MongoRestore & MongoDump

    MongoDump and MongoRestore tools are popular utilities when importing data into deployments. MongoRestore allows you to import collections, database, and deployments. The MongoRestore tool imports content from a binary database dump, created by MongoDump, into a specific database. MongoRestore can import content to an existing database or create a new one.

    MongoRestore only performs inserts into the existing database and does not perform updates or upserts. If existing data with the same _id already exists on the target database, MongoRestore will not replace it.

    To import data into your ScaleGrid MongoDB deployment utilizing MongoRestore, the following command line can be used:

    mongorestore --host <host> --port <port> --username admin --password <pass> /location/to/mongodumpfiles
  2. db.copyDatabase()

    db.copyDatabase is a easy-to-use command built into the MongoShell which allows you to easily migrate and copy data from one MongoDB instance or deployment to another. The db.copyDatabase command also supports username and passwords, making copying of databases across deployments a breeze. Follow these steps to copy a database from the remote server:

    1. Connect to your MongoDB deployment using the mongo shell. ie. mongo <host> -u <user> -p <password>
    2. Run the following command – “db.copyDatabase(“<NewDBName”, “<SourceDBName>”, “<host>”, “<username>”, “<password>”); “
  3. Snapshots

    Snapshots provide an easy way to store large amount of MongoDB data and provides point-in-time snapshots that are great to restore MongoDB deployments from scratch. ScaleGrid’s MongoDB deployments support the restoration of snapshots. NOTE: Restoring of snapshots using the following commands will overwrite all your existing data. We highly recommend you backup the existing data before overwriting it with the snapshot.

    1. SSH to the MongoDB instance you want to restore the snapshot at. More information on how to get credentials for your deployment can be found here.
    2. Stop the MongoDB service – “sudo service mongod stop”
    3. Delete all the data in mongodb_data folder – “sudo /bin/rm -rf /mongodb_data/*”
    4. Copy your snapshot to the mongodb_data folder – “sudo mv -r snapshot/* /mongodb_data/”
    5. Start the MongoDB service – “sudo service mongod start”

Thats it! It’s that simple. We’ll continue to monitor and manage the MongoDB deployment. You can backup, restore or clone this MongoDB deployment through ScaleGrid utilizing the new snapshot data.

Also, check out our new help doc on Importing MongoDB Data From a Remote Server, and how to Migrate Your mLab MongoDB Cluster to ScaleGrid.


Dharshan is the founder of ScaleGrid.io (formerly MongoDirector.com). He is an experienced MongoDB developer and administrator. He can be reached for further comment at @dharshanrg


0 Shares
+1
Tweet
Share
Share
Pin