Ruby 操作 MongoDB(2)

2021-10-19 11:51:17 浏览数 (1)

不同版本 MongoDB 和 不同版本 Ruby

下面这张表是在不同版本 Ruby 语言,不同版本的 MongoDB 中此 Ruby Driver (mongo 2.2.5) 是否兼容的列表

Ruby Version

2.4.x

2.6.x

3.0.x

MRI 1.8.x

No

No

No

MRI 1.9.x

Yes

Yes

Yes

MRI 2.0.x

Yes

Yes

Yes

MRI 2.1.x

Yes

Yes

Yes

MRI 2.2.x

Yes

Yes

Yes

JRuby 1.7.x

Yes

Yes

Yes

Note: 之所以这么强调兼容性,是要尽量在生产中避免由于兼容产生的隐患,自己写的小工具出现问题还可以随便改换过来,但是生产环境下,不是那么容易获得系统停机窗口的,并且不同版本之间的小差异可能产生调用的失败,在大量代码已经完成的情况下,再次改写是很疼的,所以前期的规划很重要,尽量减少这些潜在隐患发生的可能


连接数据库

可以使用两种方式连接 mongo

代码语言:javascript复制
[root@h102 mysql]# irb
2.3.0 :001 > require 'mongo'
 => true 
2.3.0 :002 > c = Mongo::Client.new(['192.168.100.105:27017'],:database => 'post')
D, [2016-05-25T22:11:05.617907 #36607] DEBUG -- : MONGODB | Adding 192.168.100.105:27017 to the cluster.
 => #<Mongo::Client:0x22859300 cluster=192.168.100.105:27017> 
2.3.0 :003 > c1 = Mongo::Client.new('mongodb://192.168.100.105:27017/post')
D, [2016-05-25T22:11:13.529810 #36607] DEBUG -- : MONGODB | Adding 192.168.100.105:27017 to the cluster.
 => #<Mongo::Client:0x26735480 cluster=192.168.100.105:27017> 
2.3.0 :004 > c.inspect
 => "#<Mongo::Client:0x22859300 cluster=192.168.100.105:27017>" 
2.3.0 :005 > c1.inspect
 => "#<Mongo::Client:0x26735480 cluster=192.168.100.105:27017>" 
2.3.0 :006 > c.itself
 => #<Mongo::Client:0x22859300 cluster=192.168.100.105:27017> 
2.3.0 :007 > c1.itself
 => #<Mongo::Client:0x26735480 cluster=192.168.100.105:27017> 
2.3.0 :008 >
2.3.0 :009 > c.class
 => Mongo::Client 
2.3.0 :010 > c1.class
 => Mongo::Client 
2.3.0 :011 >

Tip: 创建连接的过程中可以添加很多其它的选项,以修改初始化连接的特性,详细可以参考 Client OptionsRuby Options 还有 Details on timeout options


插入数据

插入一条数据

代码语言:javascript复制
2.3.0 :025 > r = c[:abctest].insert_one({name: 'justfortest'})
D, [2016-05-25T22:23:11.090176 #36607] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.insert | STARTED | {"insert"=>"abctest", "documents"=>[{:name=>"justfortest", :_id=>BSON::ObjectId('5745b54ff677048eff545bc5')}], "ordered"=>true}
D, [2016-05-25T22:23:11.095860 #36607] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.insert | SUCCEEDED | 0.005496831s
 => #<Mongo::Operation::Result:26962180 documents=[{"ok"=>1, "n"=>1}]> 
2.3.0 :026 > r.n
 => 1 
2.3.0 :027 > r.class
 => Mongo::Operation::Write::Insert::Result 
2.3.0 :028 >

查看插入结果的反馈

代码语言:javascript复制
2.3.0 :040 > r.ok?
 => true 
2.3.0 :041 > r.one?
 => true 
2.3.0 :042 > r.display
#<Mongo::Operation::Write::Insert::Result:0x0000000336d208> => nil 
2.3.0 :043 > r.inspect
 => "#<Mongo::Operation::Result:26962180 documents=[{"ok"=>1, "n"=>1}]>" 
2.3.0 :044 > 

插入多条数据

代码语言:javascript复制
2.3.0 :063 > r = c[:abctest].insert_many([{:name => 'abc'},{:name => 'def' },{:name => 'ghi' },{:name => 'jkl'}])
D, [2016-05-25T22:30:43.872507 #36607] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.insert | STARTED | {"insert"=>"abctest", "documents"=>[{:name=>"abc", :_id=>BSON::ObjectId('5745b713f677048eff545bca')}, {:name=>"def", :_id=>BSON::ObjectId('5745b713f677048eff545bcb')}, {:name=>"ghi", :_id=>BSON::ObjectId('5745b713f677048eff545bcc')}, {:name=>"jkl", :_...
D, [2016-05-25T22:30:43.886054 #36607] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.insert | SUCCEEDED | 0.01328074s
 => #<Mongo::BulkWrite::Result:0x000000033b0058 @results={"n_inserted"=>4, "n"=>4, "inserted_ids"=>[BSON::ObjectId('5745b713f677048eff545bca'), BSON::ObjectId('5745b713f677048eff545bcb'), BSON::ObjectId('5745b713f677048eff545bcc'), BSON::ObjectId('5745b713f677048eff545bcd')]}> 
2.3.0 :064 > r.inspect
 => "#<Mongo::BulkWrite::Result:0x000000033b0058 @results={"n_inserted"=>4, "n"=>4, "inserted_ids"=>[BSON::ObjectId('5745b713f677048eff545bca'), BSON::ObjectId('5745b713f677048eff545bcb'), BSON::ObjectId('5745b713f677048eff545bcc'), BSON::ObjectId('5745b713f677048eff545bcd')]}>" 
2.3.0 :065 > r.class
 => Mongo::BulkWrite::Result 
2.3.0 :066 > 

Tip: 根据文档中的 .n 其实已经没有了,如果调用会出现如下报错

代码语言:javascript复制
2.3.0 :077 > r.n
NoMethodError: undefined method `n' for #<Mongo::BulkWrite::Result:0x000000033b0058>
	from (irb):77
	from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :078 > 

查询数据

代码语言:javascript复制
[root@h102 ~]# irb
2.3.0 :001 > require 'mongo'
 => true 
2.3.0 :002 > c = Mongo::Client.new(['192.168.100.105:27017'],:database => 'post')
D, [2016-05-26T11:53:51.447336 #5174] DEBUG -- : MONGODB | Adding 192.168.100.105:27017 to the cluster.
 => #<Mongo::Client:0x9375760 cluster=192.168.100.105:27017> 
2.3.0 :003 >  c[:abctest].find(:name => 'abc').each do |x|
2.3.0 :004 >     puts x.class, x["_id"],x["name"]
2.3.0 :005?>   end
D, [2016-05-26T11:54:18.273709 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"abc"}}
D, [2016-05-26T11:54:18.280939 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.006918876s
BSON::Document
5745b6aaf677048eff545bc6
abc
BSON::Document
5745b713f677048eff545bca
abc
 => #<Enumerator: #<Mongo::Cursor:0x13268780 @view=#<Mongo::Collection::View:0x9305440 namespace='post.abctest' @filter={"name"=>"abc"} @options={}>>:each> 
2.3.0 :006 >

0 人点赞