Mongomapper:将文档复制到新文档中

发布于 2024-10-20 17:43:33 字数 266 浏览 0 评论 0原文

我有一个带有嵌入文档的 mongomapper 文档,并且想要复制它。

本质上,我想做的是这样的:

customer = Customer.find(params[:id])
new_customer = Customer.new
new_customer = customer
new_customer.save

所以我想最终得到两个不同的 mongomapper 文档,但内容相同。

有什么想法应该如何做到这一点?

I have a mongomapper document with embedded documents, and want to make a copy of it.

In essence, what I am trying to do is something like this:

customer = Customer.find(params[:id])
new_customer = Customer.new
new_customer = customer
new_customer.save

So I want to end up with two different mongomapper documents, but with identical content.

Any ideas how this should be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈甜 2024-10-27 17:43:33

为此,您需要更改 _id。具有相同 _id 的文档被假定为同一文档,因此使用不同 _id 保存文档将创建一个新文档。

customer = Customer.find(params[:id])
customer._id = BSON::ObjectId.new # Change _id to make a new record
  # NOTE: customer will now persist as a new document like the new_document 
  # described in the question.
customer.save # Save the new object

顺便说一句,我倾向于将旧的 _id 存储在新记录中的某个位置,以便我可以跟踪谁派生于谁,但这不是必要的。

To accomplish this, you need to change the _id. Documents with the same _id are assumed to be the same document so saving a document with a different _id will create a new document.

customer = Customer.find(params[:id])
customer._id = BSON::ObjectId.new # Change _id to make a new record
  # NOTE: customer will now persist as a new document like the new_document 
  # described in the question.
customer.save # Save the new object

As an aside, I would be inclined to store the old _id somewhere in the new record so I could keep track of who derived from who, but it is not necessary.

书信已泛黄 2024-10-27 17:43:33

您应该能够简单地执行此操作:

duplicate_doc = doc.clone
duplicate_doc.save

You should simply be able to do this:

duplicate_doc = doc.clone
duplicate_doc.save
腻橙味 2024-10-27 17:43:33

我认为不可能(或有效)在 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

这实际上在我的场景中效果很好。但我很好奇人们是否有不同的解决方案。

I don't think it is possible (or valid) to create copies of an existing document in mongodb/mongomapper because it seems to me that there would be a clash of the document/embedded documents and their ids of the original and copied documents.

So I solved my problem by copied the contents of the documents into new documents, rather than the documents themselves. Here is a sample:

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

This actually worked very well in my scenario. But I am curious if people have a different solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文