Я не думаю, что возможно (или допустимо) создать копии существующего документа в mongodb / mongomapper, потому что мне кажется, что будет столкновение документа / встроенных документов и их идентификаторов оригинала ископированные документы.
Поэтому я решил свою проблему, скопировав содержимое документов в новые документы, а не сами документы.Вот пример:
inspection = Inspection.find(params[:inspection_id]) #old document
new_inspection = Inspection.create #new target document
items = inspection.items #get the embedded documents from inspection
items.each do |item| #iterate through embedded documents
new_item = Item.create #create a new embedded document in which
# to copy the contents of the old embedded document
new_item.area_comment = item.area_comment #Copy contents of old doc into new doc
new_item.area_name = item.area_name
new_item.area_status = item.area_status
new_item.clean = item.clean
new_item.save #Save new document, it now has the data of the original
new_inspection.items << new_item #Embed the new document into its parent
end
new_inspection.save #Save the new document, its data are a copy of the data in the original document
Это на самом деле очень хорошо работало в моем сценарии.Но мне любопытно, если у людей есть другое решение.