... a simple yet elegant and quick way to move your data from MySQL to MogoDB for playing with...

After enjoying some time with MongoDB earlier I decided to instead of manually adding data, just take an existing database in MySQL and move it over to MongoDB; and from there you can truly decide for yourself right?

29 lines later, I have my data and can index it and start using it immediately.

Requirements:

  • Ruby
  • Sequel
  • MongoDB
1. Provided you have mongod running, and the ruby mysql gem installed as well as the mongo gem, you can copy and paste this rather simple script and adapt it to your needs.

#!/usr/bin/ruby
require ‘rubygems’
require ‘sequel’
require ‘mongo’
require ‘pp’
include Mongo
v = []
@db2 = Mongo::Connection.new(“localhost”).db(‘database’)
coll = @db2.collection(‘collection’)
coll.clear
@db = Sequel.mysql(
‘database’
:user => “user”
:password => “password”,
:host => “localhost”
)
@db [
“select id, column1, column2, column3 from table”
].all do |query|
b = {
:id => query[:id],
:column1 => query[:column1],
:column2 => query[:column2],
:column3 => query[:column3],
}
v.push(b)
end
coll.insert(v)

2. sit back and now enjoy.

Posted by scottmlikens on Monday, October 19, 2009