MongoDB

# MongoDB Exporting and Importing Databases

Since database creation is a hectic and sometimes manual process,it cannot be done again and again.Hence, we need a way to transfer a database or a collection from one system to another system.vThe commands we would use here are mongoexport and mongoimport.

The process to transfer a collection's data would be:

  1. Exporting the data and creating JSON file for the collection
  2. Moving that JSON file to the system where you want to export the data.
  3. Importing the data through those JSON files.

Suppose you have collection named 'clients' in a database named 'dropdeliver'. The first step was to export the data and create its JSON file.

Open the Command Prompt if you are using Windows,or Terminal if you are using MAC OS and type in the following command:

mongoexport -d databasename -c collectionname -o  path-for-json-file

For this example:

mongoexport -d dropdeliver -c clients -o  C:/json/client.json

If you get an error like:

This means that mongoexport is not avaiable at that location. So you need to change your folder and go into the bin folder of MongoDB in the Program Files directory.

If your command runs successfully you will get something like:

Now,the second step is copying the JSON file 'clients.json' to the system you want to export the database in. After you have copied the file,we just have to import the data from the JSON file into MongoDB.

Like mongoexport command,for some people this command will be accessible from the bin folder. The command is like:

mongoimport -d databasename -c collectionname path-to-json-file

For this example:

mongoimport -d dropdeliver -c clients C:/json/clients.json

If your command runs successfully,you will have something like:

Note:While importing you can choose a different database name and collection namefrom the source.Just keep the path to the JSON file correct. Thank You 😃



By Gaurav Israni

@gaurav.israni


Learn More


  • MongoDB Exporting and Importing Databases