mongodb/mongomapperに既存のドキュメントのコピーを作成することはできないと思いますが、ドキュメント/埋め込みドキュメントとオリジナルのIDが衝突するようですおよびコピーされたドキュメント。
私は自分の問題を、ドキュメントの内容ではなく新しいドキュメントにコピーして解決しました。ここにサンプルがあります:
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
これは実際に私のシナリオで非常にうまくいきました。しかし、人々が異なる解決策を持っているのであれば私は不思議です。
を、私は彼らを取得し、これを行うための唯一の方法は、親ドキュメント内に埋め込まれた文書をループで把握します属性を追加するには、ドキュメントのコピーがあるまで、これらの属性をそれぞれコピーして新しいドキュメントを作成します。誰か他の方法を考えることができますか? – futureshocked