ruby Mongodb CRUD_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > ruby Mongodb CRUD

ruby Mongodb CRUD

 2013/9/6 1:14:29  michael_roshen  程序员俱乐部  我要评论(0)
  • 摘要:require"rubygems"require"mongo"#makeaconnection#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
  • 标签:Ruby MongoDB

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

?

上一篇: DEBUG调试问题工具类-获取方法调用堆 下一篇: 没有下一篇了!
发表评论
用户名: 匿名