Mongoid migration update fields from String to Timestamp without using Models

75 Views Asked by At

I want to update a field of a collection, that actually has a string representation of a date, and change it to an ISODate.

I have a solution already, but It's is inefficient:

class MoveTimestampsFromStringToTimestamp < Mongoid::Migration
  def self.up
    self[:stats].find({"timestamp"=>{"$type"=>2}}).each do |stat|
      stat["timestamp"] = DateTime.parse(stat["timestamp"])
      self[:stats].find({_id: stat["_id"]}).update({"$set" => stat})
    end
  end
end

I would like to update it in one operation instead of search and update for every record.

0

There are 0 best solutions below