2008-12-06 00:02:51 +00:00
require " rubygems "
class Exception
def errmsg
" %s: %s \n %s " % [ self . class , message , ( backtrace || [ ] ) . join ( " \n " ) << " \n " ]
end
end
2009-08-20 14:50:48 +00:00
2008-12-06 00:02:51 +00:00
$LOAD_PATH [ 0 , 0 ] = File . join ( File . dirname ( __FILE__ ) , '..' , 'lib' )
require 'mongo'
2009-08-20 14:50:48 +00:00
include Mongo
2008-12-06 00:02:51 +00:00
2008-12-29 22:11:44 +00:00
host = ENV [ 'MONGO_RUBY_DRIVER_HOST' ] || 'localhost'
2009-08-20 22:48:09 +00:00
port = ENV [ 'MONGO_RUBY_DRIVER_PORT' ] || Connection :: DEFAULT_PORT
2008-12-06 00:02:51 +00:00
puts " >> Connecting to #{ host } : #{ port } "
2009-08-20 22:48:09 +00:00
DB = Connection . new ( host , port ) . db ( 'ruby-mongo-blog' )
2008-12-06 00:02:51 +00:00
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-29 22:11:44 +00:00
users . find ( { } , :sort = > [ { 'login' = > 1 } ] ) . 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-29 22:11:44 +00:00
articles . find ( { } , :sort = > [ { 'title' = > 1 } ] ) . 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
2009-01-06 15:47:29 +00:00
puts " closed "