2011-12-01 35 views
0

我打算遷移舊數據庫字段rake任務到Hash遷移蒙戈數據與耙狀任務

task :degrees => :environment do 
    Person.all.each do |p| 
    if p['degree1'] || p['degree2'] 
     p.degrees = {} 
     p.degrees["first"] = p['degree1'] == "Other" ? p['degree1_other'] : p['degree1'] 
     p.degrees["second"] = p['degree2'] == "Other" ? p['degree2_other'] : p['degree2'] 
     p.save 
    end 
    end 
end 

麻煩的是它與蒙戈和紅寶石佔80%和20%的CPU速度極慢分別。

對於比較簡單的遷移,我能夠使用蒙戈更新,像這樣:

db.people.update({},{$rename : {"url" : "website"}}, false, true) 

這跑速度極快。有沒有辦法將上面的rake任務轉換成mongo更新或shell腳本?

+0

如果您想要輕鬆地使用Ruby和port編寫js mongo,請從調試器中獲取mongo命令(如果您設置爲調試級別)。 – jcollum

回答

0

我創建了一個shell script

db.people.update({degree1:"Other"}, { $rename : { "degree1_other" : "degree1" } }, false, true) 
db.people.update({degree2:"Other"}, { $rename : { "degree2_other" : "degree2" } }, false, true) 
db.people.update({degree3:"Other"}, { $rename : { "degree3_other" : "degree3" } }, false, true) 

db.people.find({degrees:null}).forEach(function (doc) { 
    doc.degrees = { "first" : doc.degree1, "second" : doc.degree2, "third" : doc.degree3 }; 
    db.people.save(doc); 
}); 

db.people.update({degree1: {$exists : true}},{$unset:{degree1:1}},false,true) 
db.people.update({degree1_other: {$exists : true}},{$unset:{degree1_other:1}},false,true) 
db.people.update({degree2: {$exists : true}},{$unset:{degree2:2}},false,true) 
db.people.update({degree2_other: {$exists : true}},{$unset:{degree2_other:2}},false,true) 
db.people.update({degree3: {$exists : true}},{$unset:{degree3:3}},false,true) 
db.people.update({degree3_other: {$exists : true}},{$unset:{degree3_other:3}},false,true) 

它運行在幾秒鐘內。