Best way to write to MongoDB from a SQL Database source?
I'm trying to migrate a MySQL database over to a MongoDB database by going through the rows, getting the data, and insert the correct MongoDB information from the corresponding MySQL row. I was wondering the best way to access the raw .SQL file (in Ruby), so I can 开发者_Go百科put it into a Hash.
So there are a couple of common strategies for moving from SQL to MongoDB.
- Dump SQL DB to CSV, use
mongoimport
on CSV to pull into MongoDB. - Use existing drivers and write a script that loops through SQL DB and inserts into MongoDB.
Method #1 works for basic data sets, but it may have some problems with binary data. I generally advise against #1 unless you can confirm it's working correctly.
Method #2 is generally suggested.
The process will vary by language, but should be pretty easy. Simply query all of your data from MySQL, transform that data into the format you want for MongoDB and then perform the insert.
There are several benefits to doing it this way:
- Easy to track progress. If you have lots of data you can have your script log progress.
- Easy to re-run. It's quite possible that you'll need to re-run portions of these "dumps". Having a repeatable script is desirable.
- Tests your code. You're eventually going to need to insert new items into MongoDB once you switch over. This is a great way to test that your object building is working.
- Schema changes. It is very unlikely that your MongoDB schema will be exactly like your MySQL schema. In fact you probably don't want it to be like your MySQL schema. You will probably be merging and combining data into your new data structures. You'll want to pull this together all at once.
精彩评论