Migrating data from MongoDB to MySQL in one line of bash

Converting files using our API usually takes three steps:

  • Uploading your file
  • Running the export step on the uploaded data
  • Reading back the exported file

Well, that is not the case anymore, because we’re introducing the return_output option, which means you can convert a file in one HTTP call.

If your data is less than 100 MB, all you need to do is pass the return_output option to the step runner, like this:

$ curl https://sqlify.io/api/step/sqlify/export-sql/run \
     -u "{API_KEY}:" \
     -F "file=@file.json" \
     -F "return_output=true" > result.json

The curl command will read file.json and if successful, will return the converted SQL data in the HTTP response.

This means that you could do very interesting one liners like this one:

$ mongoexport --db database --collection data && \
  curl https://sqlify.io/api/step/sqlify/export-sql/run \
     -u "{API_KEY}:" \
     -F "file=@-" \
     -F "return_output=true" \
     -F "table_name=table" \
     -F "create_table=true" && \
  mysql -u username -p database

What this does is:

  • Export MongoDB data from your collection as JSON using mongoexport
  • Call the export-sql step to convert to runnable SQL
  • Pipe the result into your MySQL server to import the data