Created
February 21, 2012 00:20
-
-
Save coderoshi/1872516 to your computer and use it in GitHub Desktop.
MongoHQ Ruby Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gem install mongo bson_ext json | |
require 'rubygems' # if less than Ruby 1.9 | |
require 'mongo' | |
require 'uri' | |
require 'json' | |
def get_connection | |
return @db_connection if @db_connection | |
db = URI.parse(ENV['MONGOHQ_URL']) | |
db_name = db.path.gsub(/^\//, '') | |
@db_connection = Mongo::Connection.new(db.host, db.port).db(db_name) | |
@db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?) | |
@db_connection | |
end | |
db = get_connection | |
puts "Collections" | |
puts "===========" | |
collections = db.collection_names | |
puts collections | |
last_collection = collections[-1] | |
coll = db.collection(last_collection) | |
# just show 5 | |
docs = coll.find().limit(5) | |
puts "\nDocuments in #{last_collection}" | |
puts " #{docs.count()} documents(s) found" | |
puts "==========================" | |
docs.each{ |doc| puts doc.to_json } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment