修改查询特性
2.3.0 :076 > c[:abctest].find().each do |x|
2.3.0 :077 > printf("%s\t=>\t%s\n",x["_id"],x["name"])
2.3.0 :078?> end
D, [2016-05-26T13:47:08.884140 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{}}
D, [2016-05-26T13:47:08.886403 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.002051126s
5745b4aaf677048eff545bc4 => justfortest
5745b54ff677048eff545bc5 => justfortest
5745b6aaf677048eff545bc6 => abc
5745b6aaf677048eff545bc7 => def
5745b6aaf677048eff545bc8 => ghi
5745b6aaf677048eff545bc9 => jkl
5745b713f677048eff545bca => abc
5745b713f677048eff545bcb => def
5745b713f677048eff545bcc => ghi
5745b713f677048eff545bcd => jkl
=> #<Enumerator: #<Mongo::Cursor:0x9358000 @view=#<Mongo::Collection::View:0x9377420 namespace='post.abctest' @filter={} @options={}>>:each>
2.3.0 :079 > c[:abctest].find().skip(3).limit(5).each do |x|
2.3.0 :080 > printf("%s\t=>\t%s\n",x["_id"],x["name"])
2.3.0 :081?> end
D, [2016-05-26T13:47:28.682917 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{}, "skip"=>3, "limit"=>5}
D, [2016-05-26T13:47:28.686809 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.003669962s
5745b6aaf677048eff545bc7 => def
5745b6aaf677048eff545bc8 => ghi
5745b6aaf677048eff545bc9 => jkl
5745b713f677048eff545bca => abc
5745b713f677048eff545bcb => def
=> #<Enumerator: #<Mongo::Cursor:0x9118740 @view=#<Mongo::Collection::View:0x9128780 namespace='post.abctest' @filter={} @options={"skip"=>3, "limit"=>5}>>:each>
2.3.0 :082 > c[:abctest].find({:name => 'abc'}).limit(1).each do |x|
2.3.0 :083 > printf("%s\t=>\t%s\n",x["_id"],x["name"])
2.3.0 :084?> end
D, [2016-05-26T13:48:38.397344 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"abc"}, "limit"=>1}
D, [2016-05-26T13:48:38.400765 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.003194933s
5745b6aaf677048eff545bc6 => abc
=> #<Enumerator: #<Mongo::Cursor:0x7675560 @view=#<Mongo::Collection::View:0x7750520 namespace='post.abctest' @filter={"name"=>"abc"} @options={"limit"=>1}>>:each>
2.3.0 :085 >
Tip: 创建查询的过程中可以添加很多其它选项,以修改查询的特性,详细可以参考 Query Options 和 Additional Query Operations
2.3.0 :085 > c[:abctest].find({:name => 'abc'}).count
D, [2016-05-26T13:52:22.366743 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.count | STARTED | {"count"=>"abctest", "query"=>{"name"=>"abc"}}
D, [2016-05-26T13:52:22.369058 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.count | SUCCEEDED | 0.0015160509999999998s
=> 2
2.3.0 :086 > c[:abctest].find.distinct(:name)
D, [2016-05-26T13:53:19.399217 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.distinct | STARTED | {"distinct"=>"abctest", "key"=>"name", "query"=>{}}
D, [2016-05-26T13:53:19.402953 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.distinct | SUCCEEDED | 0.003509715s
=> ["justfortest", "abc", "def", "ghi", "jkl"]
2.3.0 :087 >
我们可以直接使用 Collection 来引用操作
2.3.0 :118 > a = c[:abctest]
=> #<Mongo::Collection:0x12898100 namespace=post.abctest>
2.3.0 :119 > a.class
=> Mongo::Collection
2.3.0 :120 > a.inspect
=> "#<Mongo::Collection:0x12898100 namespace=post.abctest>"
2.3.0 :121 > a.name
=> "abctest"
2.3.0 :122 > a.namespace
=> "post.abctest"
2.3.0 :123 > a.itself
=> #<Mongo::Collection:0x12898100 namespace=post.abctest>
2.3.0 :124 > a.cluster
=> #<Mongo::Cluster:0x9375440 servers=[#<Mongo::Server:0x9373660 address=192.168.100.105:27017>] topology=Single>
2.3.0 :125 > a.client
=> #<Mongo::Client:0x9375760 cluster=192.168.100.105:27017>
2.3.0 :126 > a.count
D, [2016-05-26T14:46:11.460020 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.count | STARTED | {"count"=>"abctest", "query"=>{}}
D, [2016-05-26T14:46:11.463027 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.count | SUCCEEDED | 0.002638529s
=> 10
2.3.0 :127 >
2.3.0 :173 > a.find(:name => 'justfortest').each do |x|
2.3.0 :174 > puts x
2.3.0 :175?> end
D, [2016-05-26T15:41:11.761639 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"justfortest"}}
D, [2016-05-26T15:41:11.766917 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.005021206s
{"_id"=>BSON::ObjectId('5745b4aaf677048eff545bc4'), "name"=>"justfortest"}
{"_id"=>BSON::ObjectId('5745b54ff677048eff545bc5'), "name"=>"justfortest"}
=> #<Enumerator: #<Mongo::Cursor:0x13474400 @view=#<Mongo::Collection::View:0x13478700 namespace='post.abctest' @filter={"name"=>"justfortest"} @options={}>>:each>
2.3.0 :176 > a.find(:name => 'justfortest').update_one("$inc" => { :newfiled => 1 })
D, [2016-05-26T15:43:04.517881 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.update | STARTED | {"update"=>"abctest", "updates"=>[{"q"=>{"name"=>"justfortest"}, "u"=>{"$inc"=>{:newfiled=>1}}, "multi"=>false, "upsert"=>false}], "ordered"=>true}
D, [2016-05-26T15:43:04.520336 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.update | SUCCEEDED | 0.002116027s
=> #<Mongo::Operation::Result:13420760 documents=[{"ok"=>1, "nModified"=>1, "n"=>1}]>
2.3.0 :177 > a.find(:name => 'justfortest').each do |x|
2.3.0 :178 > puts x
2.3.0 :179?> end
D, [2016-05-26T15:43:13.598368 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"justfortest"}}
D, [2016-05-26T15:43:13.602324 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.003671943s
{"_id"=>BSON::ObjectId('5745b4aaf677048eff545bc4'), "name"=>"justfortest", "newfiled"=>1}
{"_id"=>BSON::ObjectId('5745b54ff677048eff545bc5'), "name"=>"justfortest"}
=> #<Enumerator: #<Mongo::Cursor:0x70030381400740 @view=#<Mongo::Collection::View:0x70030381405400 namespace='post.abctest' @filter={"name"=>"justfortest"} @options={}>>:each>
2.3.0 :180 >
2.3.0 :184 > r = a.find(:name => 'justfortest').update_one("$inc" => { :newfiled => 1 })
D, [2016-05-26T15:45:17.110646 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.update | STARTED | {"update"=>"abctest", "updates"=>[{"q"=>{"name"=>"justfortest"}, "u"=>{"$inc"=>{:newfiled=>1}}, "multi"=>false, "upsert"=>false}], "ordered"=>true}
D, [2016-05-26T15:45:17.112755 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.update | SUCCEEDED | 0.001860819s
=> #<Mongo::Operation::Result:70030381342520 documents=[{"ok"=>1, "nModified"=>1, "n"=>1}]>
2.3.0 :185 > a.find(:name => 'justfortest').each do |x|
2.3.0 :186 > puts x
2.3.0 :187?> end
D, [2016-05-26T15:45:31.859453 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"justfortest"}}
D, [2016-05-26T15:45:31.865512 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.005811567s
{"_id"=>BSON::ObjectId('5745b4aaf677048eff545bc4'), "name"=>"justfortest", "newfiled"=>2}
{"_id"=>BSON::ObjectId('5745b54ff677048eff545bc5'), "name"=>"justfortest"}
=> #<Enumerator: #<Mongo::Cursor:0x13244260 @view=#<Mongo::Collection::View:0x13256880 namespace='post.abctest' @filter={"name"=>"justfortest"} @options={}>>:each>
2.3.0 :188 > r.n
=> 1
2.3.0 :189 > r.class
=> Mongo::Operation::Write::Update::Result
2.3.0 :190 >
2.3.0 :191 > r.ok?
=> true
2.3.0 :192 >
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。