在 Mongoid 中,如何将嵌入文档传输到不同的父文档?
我正在使用下面的代码,但我不想删除/复制。我曾经能够更改父 ID,但 Mongoid/MongoDB 中的嵌入文档不存在这种情况
def move(new_parent)
if self._parent != new_parent
copy = self.dup
self.delete
new_parent.items << copy
end
end
I am using the following code below, but I would prefer not to have to delete/copy. I used to be able to change the parent ID, but that doesn't exist for embedded documents in Mongoid/MongoDB
def move(new_parent)
if self._parent != new_parent
copy = self.dup
self.delete
new_parent.items << copy
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的理解是嵌入文档作为属性存储在父文档中 - 它们没有parent_ids,因为它们实际上是父文档的一部分(因此,“嵌入”)。因此,重新定义它们的唯一方法是克隆和克隆它们。删除 - 就像你所做的那样。
您可能可以将方法减少一行,但仅此而已。
My understanding is the embedded documents are stored as attributes inside your parent documents - they don't have parent_ids, since they are actually part of their parent (hence, 'embedded'). As such, the only way to reparent them would be to clone & delete - as you've done.
You could probably cut your method down by one line, but that's about it.