Working on more examples
This commit is contained in:
parent
33c1702596
commit
66c3ed3b6e
|
@ -0,0 +1,78 @@
|
|||
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
|
||||
|
||||
LINE_SIZE = 120
|
||||
|
||||
puts ">> Connecting to #{host}:#{port}"
|
||||
DB = Mongo.new(host, port).db('ruby-mongo-blog')
|
||||
|
||||
puts "=" * LINE_SIZE
|
||||
puts "Adding authors"
|
||||
authors = DB.collection "authors"
|
||||
authors.clear
|
||||
# authors.create_index "meta", :_id => 1, :name => 1, :age => 1
|
||||
puts "-" * LINE_SIZE
|
||||
borges = authors << { :name => "Jorge Luis Borges", :email => "jorge@borges.com", :age => 123 }
|
||||
puts "borges : #{borges.inspect}"
|
||||
shaksp = authors << { :name => "William Shakespeare", :email => "william@shakespeare.com", :age => 587 }
|
||||
puts "shaksp : #{shaksp.inspect}"
|
||||
puts "-" * LINE_SIZE
|
||||
puts "authors ordered by age ascending"
|
||||
puts "-" * LINE_SIZE
|
||||
authors.find({}, nil, :sort => :age).each {|x| puts "%-25.25s : %-25.25s : %3i" % [x['name'], x['email'], x['age']]}
|
||||
|
||||
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
|
||||
users.find({}, nil, :sort => :login).each {|x| puts "%-10.10s : %-25.25s : %-25.25s" % [x['login'], x['name'], x['email']]}
|
||||
|
||||
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
|
||||
articles.find({}, nil, :sort => :title).each {|x| puts "%-25.25s : %-25.25s" % [x['title'], x['author_id']]}
|
||||
|
||||
puts ">> Closing connection"
|
||||
DB.close
|
||||
puts "closed"
|
Loading…
Reference in New Issue