Collections 操作
创建集合
代码语言: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-26T22:26:43.917490 #32905] DEBUG -- : MONGODB | Adding 192.168.100.105:27017 to the cluster.
=> #<Mongo::Client:0x7687020 cluster=192.168.100.105:27017>
2.3.0 :003 > newtable = c[:newtable, :capped => true, :size => 1024]
=> #<Mongo::Collection:0x11552180 namespace=post.newtable>
2.3.0 :004 >
此时进行本地检查
代码语言:javascript复制> show tables;
abctest
post
user
users
>
还有没生成表
然后我进行创建
代码语言:javascript复制2.3.0 :004 > newtable.create
D, [2016-05-26T22:28:34.677356 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.create | STARTED | {"create"=>"newtable", "capped"=>true, "size"=>1024}
D, [2016-05-26T22:28:34.740613 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.create | SUCCEEDED | 0.062936352s
=> #<Mongo::Operation::Result:11601440 documents=[{"ok"=>1.0}]>
2.3.0 :005 >
再看本地
代码语言:javascript复制> show tables;
abctest
newtable
post
user
users
>
删除集合
代码语言:javascript复制2.3.0 :005 > newtable.capped?
D, [2016-05-26T22:29:24.455870 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.collstats | STARTED | {"collstats"=>"newtable"}
D, [2016-05-26T22:29:24.464827 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.collstats | SUCCEEDED | 0.008704707s
=> true
2.3.0 :006 > newtable.drop
D, [2016-05-26T22:30:47.021025 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.drop | STARTED | {"drop"=>"newtable"}
D, [2016-05-26T22:30:47.030352 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.drop | SUCCEEDED | 0.00903782s
=> #<Mongo::Operation::Result:11770800 documents=[{"ns"=>"post.newtable", "nIndexesWas"=>1, "ok"=>1.0}]>
2.3.0 :007 > newtable.capped?
D, [2016-05-26T22:31:18.597838 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.collstats | STARTED | {"collstats"=>"newtable"}
D, [2016-05-26T22:31:18.601270 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.collstats | FAILED | Collection [post.newtable] not found. () | 0.003093439s
Mongo::Error::OperationFailure: Collection [post.newtable] not found. ()
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/operation/result.rb:256:in `validate!'
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/operation/executable.rb:36:in `block in execute'
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool.rb:108:in `with_connection'
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/server/context.rb:63:in `with_connection'
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/operation/executable.rb:34:in `execute'
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/database.rb:158:in `command'
from /usr/local/rvm/gems/ruby-2.3.0/gems/mongo-2.2.5/lib/mongo/collection.rb:162:in `capped?'
from (irb):7
from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :008 >
本地查看,发现 newtable 没了
代码语言:javascript复制> show tables;
abctest
post
user
users
>
还可以修改读写的倾向性,可以参考 Changing Read/Write Preferences