One-liner to migrate data from MongoDB to MySQL from your shell

Our JSON to SQL converstion tool can make MongoDB migrations a breeze.

The first thing you want to do is export the MongoDB data from your collection as JSON using mongoexport, remember to replace database and data with your database and collection names.

$ mongoexport --db database --collection data > data.json

Once you have your exported data as a file, it’s time to convert it to SQL, you can do this in two ways:

We’re going to cover the API method in this article but you can also use the web UI to achieve the same result.

Make sure to sign up to get a free API key, you can continue once you’ve got your key.

Once you have your key, use the convert to sql step to convert the JSON to runnable SQL, configure what type of SQL you need (PostgreSQL, MySQL etc.), the table name and wether you would like us to create the table.

$ curl https://api.konbert.com/v1/pipelines/sqlify/convert/run \
   -u "{API_KEY}:" \
   -F "file=@data.json" \
   -F "return_output=true" \
   -F "options[to]=sql" \
   -F "options[output][flavour]=mysql" \
   -F "options[output][table_name]=table" \
   -F "options[output][create_table]=true" > data.sql

Check out the API reference for the convert endpoint for more advanced options here.

Then pipe the result into your MySQL server to import the data

$ mysql -u username -p database < data.sql

One-liner

With this, it means that you can do very interesting one liners like this one:

$ mongoexport --db database --collection data | \
  curl https://api.konbert.com/v1/pipelines/sqlify/convert/run \
     -u "{API_KEY}:" \
     -F "file=@-" \
     -F "return_output=true" \
     -F "options[to]=sql" \
     -F "options[output][flavour]=mysql" \
     -F "options[output][table_name]=table" \
     -F "options[output][create_table]=true" | \
  mysql -u username -p database

Happy migrating!