How would you go about deep cloning a document in MongoDB (mongoid)
I've tried something like this;
original = Car.find(old_id)
@car = original.clone
@car._id = BSON::ObjectId.new
But I get problems deserialization of the values afterwards.
How can I make a deep clone with all the documents attributes except the _id?
Edit: After following Zachary's example I got some problems with a custom serialization class for the duplicated documents.
class OptionHash
include Mongoid::Fields::Serializable
# Convert the keys from Strings to Symbols
def deserialize(object)
object.symbolize_keys!
end
# Convert values into Booleans
def serialize(object)
object.each do |key, value|
object[key] = Boolean::MAPPINGS[value]
end
end
Object is nil for duplicated documents. Car.find(old_id).attributes indeed doesn't include the field with the custom serialization, why is that and how can I include it?
You have to use
Car.instantiate
if you have localized fields so the code isThis solution is not very clean but i don't have found better.
You don't need to call .clone on this, you can use the raw data from
attributes
. For example the below method/example will give new ids throughout the entire document if it finds one.And you now have a copy of Car. This is inefficient though as it has to go through the entire hash for the record to figure out if there are any embedded documents in an embedded document. You would be better off resetting the structure yourself if you know how it'll be, for example, if you have a parts embedded into car, then you can just do:
Which is a lot more efficient than just doing a generic reset.