mongo-ruby-driver/examples/blog.rb

77 lines
2.6 KiB
Ruby
Raw Normal View History

2008-12-06 00:02:51 +00:00
require "rubygems"
require "benchwarmer"
class Exception
def errmsg
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
end
end
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
include XGen::Mongo::Driver
host = ARGV[0] || 'localhost'
port = ARGV[1] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
puts ">> Connecting to #{host}:#{port}"
DB = Mongo.new(host, port).db('ruby-mongo-blog')
2008-12-17 01:14:04 +00:00
LINE_SIZE = 120
2008-12-06 00:02:51 +00:00
puts "=" * LINE_SIZE
puts "Adding authors"
authors = DB.collection "authors"
authors.clear
2008-12-17 01:14:04 +00:00
authors.create_index "meta", '_id' => 1, 'name' => 1, 'age' => 1
2008-12-06 00:02:51 +00:00
puts "-" * LINE_SIZE
shaksp = authors << { :name => "William Shakespeare", :email => "william@shakespeare.com", :age => 587 }
puts "shaksp : #{shaksp.inspect}"
2008-12-08 21:41:14 +00:00
borges = authors << { :name => "Jorge Luis Borges", :email => "jorge@borges.com", :age => 123 }
puts "borges : #{borges.inspect}"
2008-12-06 00:02:51 +00:00
puts "-" * LINE_SIZE
puts "authors ordered by age ascending"
puts "-" * LINE_SIZE
2008-12-17 01:14:04 +00:00
authors.find({}, :sort => [{'age' => 1}]).each {|x| puts "%-25.25s : %-25.25s : %3i" % [x['name'], x['email'], x['age']]}
2008-12-06 00:02:51 +00:00
puts "=" * LINE_SIZE
puts "Adding users"
users = DB.collection "users"
users.clear
# users.create_index "meta", :_id => 1, :login => 1, :name => 1
puts "-" * LINE_SIZE
jdoe = users << { :login => "jdoe", :name => "John Doe", :email => "john@doe.com" }
puts "jdoe : #{jdoe.inspect}"
lsmt = users << { :login => "lsmith", :name => "Lucy Smith", :email => "lucy@smith.com" }
puts "lsmt : #{lsmt.inspect}"
puts "-" * LINE_SIZE
puts "users ordered by login ascending"
puts "-" * LINE_SIZE
2008-12-08 21:41:14 +00:00
users.find({}, :sort => :login).each {|x| puts "%-10.10s : %-25.25s : %-25.25s" % [x['login'], x['name'], x['email']]}
2008-12-06 00:02:51 +00:00
puts "=" * LINE_SIZE
puts "Adding articles"
articles = DB.collection "articles"
articles.clear
# articles.create_index "meta", :_id => 1, :author_id => 1, :title => 1
puts "-" * LINE_SIZE
begin
art1 = articles << { :title => "Caminando por Buenos Aires", :body => "Las callecitas de Buenos Aires tienen ese no se que...", :author_id => borges["_id"].to_s }
puts "art1 : #{art1.inspect}"
rescue => e
puts "Error: #{e.errmsg}"
end
begin
art2 = articles << { :title => "I must have seen thy face before", :body => "Thine eyes call me in a new way", :author_id => shaksp["_id"].to_s, :comments => [ { :user_id => jdoe["_id"].to_s, :body => "great article!" } ] }
puts "art2 : #{art2.inspect}"
rescue => e
puts "Error: #{e.errmsg}"
end
puts "-" * LINE_SIZE
puts "articles ordered by title ascending"
puts "-" * LINE_SIZE
2008-12-08 21:41:14 +00:00
articles.find({}, :sort => :title).each {|x| puts "%-25.25s : %-25.25s" % [x['title'], x['author_id']]}
2008-12-06 00:02:51 +00:00
puts ">> Closing connection"
DB.close
puts "closed"