Skip to content

Instantly share code, notes, and snippets.

@taise
Last active December 20, 2015 02:58
Show Gist options
  • Select an option

  • Save taise/6059814 to your computer and use it in GitHub Desktop.

Select an option

Save taise/6059814 to your computer and use it in GitHub Desktop.
MySQL data import to MongoDB by ruby.

MySQL

How to use mysql2

gem install mysql2
require 'mysql2'

ms_client = Mysql2::Client.new(
              host: 'localhost',
              username: 'root',
              database: 'db'
            )

results = ms_client.query('SELECT * FROM users')

results.each do |row|
  puts row
end

MongoDB

How to use mongo

gem install mongo
require 'mongo'

include Mongo

mg_client = MongoClient.new('localhost')
db = mg_client.db('db')
users = db.collection('users')

results = users.find

results.each do |row|
  puts row
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment