I have a rake task intended to migrate legacy db fields into a 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
Trouble is it’s extremely slow with mongo and ruby taking up 80% and 20% CPU respectively.
For simpler migrations I was able to use a mongo update like so:
db.people.update({},{$rename : {"url" : "website"}}, false, true)
This ran extremely fast. Is there any way to translate the above rake task into a mongo update or shell script?
I created a shell script:
It runs within a few seconds.