require "rubygems"
require "mongo"
#make a connection
#db = Mongo::Connection.new.db("mydb")
#db = Mongo::Connection.new("localhost").db("mydb")
#db = Mongo::Connection.new("localhost", 27017).db("mydb")
#
#conn = Mongo::Connection.new()
conn = Mongo::Connection.new("localhost")
puts conn.class # Mongo::Connection
puts conn.database_names
db = Mongo::Connection.new.db('TestData')
col_1 = db.collection('users')
col_2 = db["users"]
#[]是collection的别名
p col_1.count
p col_1.find_one
p col_2.find_one
#{"_id"=>BSON::ObjectId('52232ffde5cd1c851ebbcd5d'), "age"=>55.0, "name"=>"xuqi", "status"=>"a"}
#{"_id"=>BSON::ObjectId('52232ffde5cd1c851ebbcd5d'), "age"=>55.0, "name"=>"xuqi", "status"=>"a"}
#查询
db['users'].find({ "age" => {"$gt" => 53}}).each do |row|
p row
end
p "-------------------------------"
db['users'].find.each do |row|
p row
end
#插入
user = {"age"=>55.0, "name"=>"wuhao", "status"=>"a"}
col_1.insert(user)
p col_1.count
#更新
col_1.update({:name => "wuhao"}, {"$set" => {:age => 23}})
p col_1.find({:name => "wuhao"}).first
#删除
col_1.remove({:name => "wuhao"})
p col_1.count
资料:http://www.railstips.org/blog/archives/2009/06/27/mongomapper-the-rad-mongo-wrapper/
不知道用 ruby 怎么写 mapreduce, 研究中。。。