另外一种形式
2.3.0 :299 > a.find_one_and_replace({:name => 'xxx'},{:name => 'yyy'})
D, [2016-05-26T16:40:24.705708 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.findandmodify | STARTED | {"findandmodify"=>"abctest", "query"=>{"name"=>"xxx"}, "update"=>{:name=>"yyy"}, "new"=>false, "bypassDocumentValidation"=>false}
D, [2016-05-26T16:40:24.707865 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.findandmodify | SUCCEEDED | 0.0019451739999999998s
=> {"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"xxx"}
2.3.0 :300 > a.find(:name => 'xxx').to_a
D, [2016-05-26T16:40:27.641622 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"xxx"}}
D, [2016-05-26T16:40:27.646195 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.004332849999999999s
=> []
2.3.0 :301 > a.find(:name => 'yyy').to_a
D, [2016-05-26T16:40:33.807708 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"yyy"}}
D, [2016-05-26T16:40:33.810583 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.0026168429999999998s
=> [{"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"yyy"}]
2.3.0 :302 >
2.3.0 :322 > a.find(:name => 'zzz').to_a
D, [2016-05-26T16:57:07.911844 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"zzz"}}
D, [2016-05-26T16:57:07.913529 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.001439203s
=> [{"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"zzz"}]
2.3.0 :323 > a.find(:name => 'zzz').find_one_and_update('$set' => {:ui => 'CLI'})
D, [2016-05-26T16:57:43.518176 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.findandmodify | STARTED | {"findandmodify"=>"abctest", "query"=>{"name"=>"zzz"}, "update"=>{"$set"=>{:ui=>"CLI"}}, "new"=>false, "bypassDocumentValidation"=>false}
D, [2016-05-26T16:57:43.522056 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.findandmodify | SUCCEEDED | 0.00366088s
=> {"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"zzz"}
2.3.0 :324 > a.find(:name => 'zzz').to_a
D, [2016-05-26T16:57:46.291361 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"zzz"}}
D, [2016-05-26T16:57:46.294192 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.002366816s
=> [{"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"zzz", "ui"=>"CLI"}]
2.3.0 :325 >
另一种形式
可以把结果集进行保存
2.3.0 :333 > d = a.find_one_and_update({"name"=>"zzz"},{"ui"=>"GUI"})
D, [2016-05-26T17:02:15.633513 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.findandmodify | STARTED | {"findandmodify"=>"abctest", "query"=>{"name"=>"zzz"}, "update"=>{"ui"=>"GUI"}, "new"=>false, "bypassDocumentValidation"=>false}
D, [2016-05-26T17:02:15.637428 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.findandmodify | SUCCEEDED | 0.003628107s
=> {"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"zzz", "ui"=>"GUI"}
2.3.0 :334 > d.class
=> BSON::Document
2.3.0 :335 > d
=> {"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "name"=>"zzz", "ui"=>"GUI"}
2.3.0 :336 >
Note: 虽然用的是
find_one_and_update
方法,但其实这个过程是在替换,要分外小心
2.3.0 :340 > a.find({"name"=>"zzz"}).to_a
D, [2016-05-26T17:23:52.321931 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"zzz"}}
D, [2016-05-26T17:23:52.323976 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.0018150710000000001s
=> []
2.3.0 :341 > a.find({"ui"=>"GUI"}).to_a
D, [2016-05-26T17:24:14.688982 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"ui"=>"GUI"}}
D, [2016-05-26T17:24:14.691677 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.002366695s
=> [{"_id"=>BSON::ObjectId('5745b6aaf677048eff545bc8'), "ui"=>"GUI"}, {"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "ui"=>"GUI"}]
2.3.0 :342 >
2.3.0 :353 > a.find({"ui"=>"GUI"}).to_a
D, [2016-05-26T17:27:34.928755 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"ui"=>"GUI"}}
D, [2016-05-26T17:27:34.930674 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.0016437049999999999s
=> [{"_id"=>BSON::ObjectId('5745b6aaf677048eff545bc8'), "ui"=>"GUI"}, {"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "ui"=>"GUI"}]
2.3.0 :354 > r = a.find({"ui"=>"GUI"}).delete_one
D, [2016-05-26T17:29:53.396121 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | STARTED | {"delete"=>"abctest", "deletes"=>[{"q"=>{"ui"=>"GUI"}, "limit"=>1}], "ordered"=>true}
D, [2016-05-26T17:29:53.398662 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | SUCCEEDED | 0.0023097839999999996s
=> #<Mongo::Operation::Result:12774140 documents=[{"ok"=>1, "n"=>1}]>
2.3.0 :355 > r.n
=> 1
2.3.0 :356 > a.find({"ui"=>"GUI"}).to_a
D, [2016-05-26T17:30:10.157727 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"ui"=>"GUI"}}
D, [2016-05-26T17:30:10.160650 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.002679249s
=> [{"_id"=>BSON::ObjectId('5745b713f677048eff545bcb'), "ui"=>"GUI"}]
2.3.0 :357 > a.delete_one("ui"=>"GUI")
D, [2016-05-26T17:30:34.906641 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | STARTED | {"delete"=>"abctest", "deletes"=>[{"q"=>{"ui"=>"GUI"}, "limit"=>1}], "ordered"=>true}
D, [2016-05-26T17:30:34.909964 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | SUCCEEDED | 0.003001837s
=> #<Mongo::Operation::Result:10403640 documents=[{"ok"=>1, "n"=>1}]>
2.3.0 :358 > a.find({"ui"=>"GUI"}).to_a
D, [2016-05-26T17:30:43.650621 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"ui"=>"GUI"}}
D, [2016-05-26T17:30:43.653355 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.002408293s
=> []
2.3.0 :359 >
2.3.0 :359 > a.find({"name"=>"jkl"}).to_a
D, [2016-05-26T17:36:08.230221 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"jkl"}}
D, [2016-05-26T17:36:08.232321 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.0016569990000000001s
=> [{"_id"=>BSON::ObjectId('5745b6aaf677048eff545bc9'), "name"=>"jkl"}, {"_id"=>BSON::ObjectId('5745b713f677048eff545bcd'), "name"=>"jkl"}]
2.3.0 :360 > a.find({"name"=>"jkl"}).delete_many
D, [2016-05-26T17:36:38.044545 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | STARTED | {"delete"=>"abctest", "deletes"=>[{"q"=>{"name"=>"jkl"}, "limit"=>0}], "ordered"=>true}
D, [2016-05-26T17:36:38.048958 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | SUCCEEDED | 0.004232251s
=> #<Mongo::Operation::Result:9183680 documents=[{"ok"=>1, "n"=>2}]>
2.3.0 :361 > a.find({"name"=>"jkl"}).to_a
D, [2016-05-26T17:36:41.693029 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"jkl"}}
D, [2016-05-26T17:36:41.695807 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.002505381s
=> []
2.3.0 :362 >
另一种形式
2.3.0 :362 > a.find({"name"=>"replaceTest"}).to_a
D, [2016-05-26T17:37:51.179071 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"replaceTest"}}
D, [2016-05-26T17:37:51.185426 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.006037441s
=> [{"_id"=>BSON::ObjectId('5745b4aaf677048eff545bc4'), "name"=>"replaceTest"}, {"_id"=>BSON::ObjectId('5745b54ff677048eff545bc5'), "name"=>"replaceTest"}]
2.3.0 :363 > a.delete_many(:name => "replaceTest")
D, [2016-05-26T17:38:41.258842 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | STARTED | {"delete"=>"abctest", "deletes"=>[{"q"=>{"name"=>"replaceTest"}, "limit"=>0}], "ordered"=>true}
D, [2016-05-26T17:38:41.261118 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.delete | SUCCEEDED | 0.001905545s
=> #<Mongo::Operation::Result:6844980 documents=[{"ok"=>1, "n"=>2}]>
2.3.0 :364 > a.find({"name"=>"replaceTest"}).to_a
D, [2016-05-26T17:38:43.690739 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | STARTED | {"find"=>"abctest", "filter"=>{"name"=>"replaceTest"}}
D, [2016-05-26T17:38:43.692741 #5174] DEBUG -- : MONGODB | 192.168.100.105:27017 | post.find | SUCCEEDED | 0.0017209950000000001s
=> []
2.3.0 :365 >
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。