Skip to content

Instantly share code, notes, and snippets.

@jharjono
Created March 9, 2011 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jharjono/861735 to your computer and use it in GitHub Desktop.
Save jharjono/861735 to your computer and use it in GitHub Desktop.
Dump JSON documents into MongoDB
#!/usr/bin/env ruby
# Dump JSON documents into MongoDB
require 'rubygems'
require 'mongo'
require 'json'
# Grab the dataset from a dump
filename = ARGV.first
docs = JSON.parse(File.open(filename).read)
# Get handle to MongoDB
db = Mongo::Connection.new("localhost", 27017).db("mydb")
collection = db.collection("my_collection")
# Insert each document
docs.each do |doc|
collection.insert(doc)
end
puts collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment