Skip to content

Instantly share code, notes, and snippets.

@Andrzej
Created October 10, 2011 19:16
Show Gist options
  • Save Andrzej/1276244 to your computer and use it in GitHub Desktop.
Save Andrzej/1276244 to your computer and use it in GitHub Desktop.
Convert BSON file into SQL, useful for fast copy data from MongoDB to MySQL (mongodump => bson2sql => import to mysql)
require "bson"
def make_insert(table_name, bson)
columns = ["id",*bson["value"].keys] * ", "
values = ["'#{bson["_id"]}'",*bson["value"].values.map{|value| value.is_a?(Numeric) ? value : "'#{value}'"}] * ", "
return "insert into #{table_name} (#{columns}) values (#{values});"
end
file_name = ARGV.first
file=File.new(file_name)
table_name=File.basename(file_name,".*")
while not file.eof? do
bson = BSON.read_bson_document(file)
STDOUT << make_insert(table_name,bson)
STDOUT << "\n"
end
@kevpax
Copy link

kevpax commented Feb 14, 2018

bson2sql.rb:13:in <main>': undefined method read_bson_document' for BSON:Module (NoMethodError)

Has the bson module changed since this was developed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment