创建多个索引
代码语言:javascript复制2.3.0 :056 > db1[:test2].indexes.create_many([{:key => { name: 1 }, :unique => true },{:key => { label: -1 }}])
D, [2016-05-26T23:27:27.426590 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.createIndexes | STARTED | {"createIndexes"=>"test2", "indexes"=>[{:key=>{:name=>1}, :unique=>true, :name=>"name_1"}, {:key=>{:label=>-1}, :name=>"label_-1"}]}
D, [2016-05-26T23:27:27.479450 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.createIndexes | SUCCEEDED | 0.052639038s
=> #<Mongo::Operation::Result:7996660 documents=[{"createdCollectionAutomatically"=>true, "numIndexesBefore"=>1, "numIndexesAfter"=>3, "ok"=>1.0}]>
2.3.0 :057 >
本地查看
代码语言:javascript复制> show tables
abctest
test2
> db.test2.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "post.test2"
},
{
"v" : 1,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "post.test2"
},
{
"v" : 1,
"key" : {
"label" : -1
},
"name" : "label_-1",
"ns" : "post.test2"
}
]
>
创建索引过程中还可以加入其它参数,详细参数可以参考 索引参数
查看索引
代码语言:javascript复制2.3.0 :061 > db1[:test2].indexes.to_a
D, [2016-05-26T23:36:39.171127 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | STARTED | {"listIndexes"=>"test2", "cursor"=>{}}
D, [2016-05-26T23:36:39.203225 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | SUCCEEDED | 0.031920452s
=> [{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"post.test2"}, {"v"=>1, "unique"=>true, "key"=>{"name"=>1}, "name"=>"name_1", "ns"=>"post.test2"}, {"v"=>1, "key"=>{"label"=>-1}, "name"=>"label_-1", "ns"=>"post.test2"}]
2.3.0 :062 > db1[:test2].indexes.each do |i|
2.3.0 :063 > p i
2.3.0 :064?> end
D, [2016-05-26T23:37:24.375182 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | STARTED | {"listIndexes"=>"test2", "cursor"=>{}}
D, [2016-05-26T23:37:24.377380 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | SUCCEEDED | 0.00198784s
{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"post.test2"}
{"v"=>1, "unique"=>true, "key"=>{"name"=>1}, "name"=>"name_1", "ns"=>"post.test2"}
{"v"=>1, "key"=>{"label"=>-1}, "name"=>"label_-1", "ns"=>"post.test2"}
=> #<Enumerator: #<Mongo::Cursor:0x10950640 @view=#<Mongo::Index::View:0x000000014d5a20 @collection=#<Mongo::Collection:0x10923340 namespace=post.test2>, @batch_size=nil>>:each>
2.3.0 :065 >
删除索引
删除一个索引
代码语言:javascript复制2.3.0 :078 > db1[:abctest].indexes.to_a
D, [2016-05-26T23:40:04.083421 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | STARTED | {"listIndexes"=>"abctest", "cursor"=>{}}
D, [2016-05-26T23:40:04.086508 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | SUCCEEDED | 0.0028248400000000003s
=> [{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"post.abctest"}, {"v"=>1, "unique"=>true, "key"=>{"name"=>1}, "name"=>"name_1", "ns"=>"post.abctest"}]
2.3.0 :079 > db1[:abctest].indexes.drop_one('name_1')
D, [2016-05-26T23:40:30.530875 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.dropIndexes | STARTED | {"dropIndexes"=>"abctest", "index"=>"name_1"}
D, [2016-05-26T23:40:30.563482 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.dropIndexes | SUCCEEDED | 0.032385475s
=> #<Mongo::Operation::Result:7589800 documents=[{"nIndexesWas"=>2, "ok"=>1.0}]>
2.3.0 :080 > db1[:abctest].indexes.to_a
D, [2016-05-26T23:40:32.946595 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | STARTED | {"listIndexes"=>"abctest", "cursor"=>{}}
D, [2016-05-26T23:40:32.950582 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | SUCCEEDED | 0.0037632599999999996s
=> [{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"post.abctest"}]
2.3.0 :081 >
删除所有索引
代码语言:javascript复制2.3.0 :087 > db1[:test2].indexes.to_a
D, [2016-05-26T23:42:10.912880 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | STARTED | {"listIndexes"=>"test2", "cursor"=>{}}
D, [2016-05-26T23:42:10.916162 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | SUCCEEDED | 0.0028684260000000003s
=> [{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"post.test2"}, {"v"=>1, "unique"=>true, "key"=>{"name"=>1}, "name"=>"name_1", "ns"=>"post.test2"}, {"v"=>1, "key"=>{"label"=>-1}, "name"=>"label_-1", "ns"=>"post.test2"}]
2.3.0 :088 > db1[:test2].indexes.drop_all
D, [2016-05-26T23:42:19.955254 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.dropIndexes | STARTED | {"dropIndexes"=>"test2", "index"=>"*"}
D, [2016-05-26T23:42:19.979831 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.dropIndexes | SUCCEEDED | 0.024125167s
=> #<Mongo::Operation::Result:7885720 documents=[{"nIndexesWas"=>3, "msg"=>"non-_id indexes dropped for collection", "ok"=>1.0}]>
2.3.0 :089 > db1[:test2].indexes.to_a
D, [2016-05-26T23:42:27.945764 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | STARTED | {"listIndexes"=>"test2", "cursor"=>{}}
D, [2016-05-26T23:42:27.948694 #32905] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.listIndexes | SUCCEEDED | 0.0026218500000000002s
=> [{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"post.test2"}]
2.3.0 :090 >
命令汇总
gem source -l
gem install mongo
gem list | grep mongo
irb
原文地址