简体   繁体   English

深度克隆带有嵌入文档 ID(树结构)的 MongoDb 文档

[英]Deep clone a MongoDb document with embedded documents ID's (tree structure)

I am struggling on finding a solution based on deep cloning a given document which has embedded documents ID's, but those embedded documents might also have other embedded documents ID's.我正在努力寻找基于深度克隆具有嵌入式文档 ID 的给定文档的解决方案,但这些嵌入式文档也可能具有其他嵌入式文档 ID。 I want to create copies of these embedded documents in the specific collections they belong to and build up the parent(root) document that has references to these newly added documents.我想在它们所属的特定 collections 中创建这些嵌入文档的副本,并构建引用这些新添加文档的父(根)文档。 So for example I have this schema:所以例如我有这个模式:

1. page 1.页面

{ 
"_id" : 123456789, 
"page_title" : "Title 1",
"sections" : [111, 222, ...]
"variables" : [v1, v2, ...]
}

2. sections 2. 部分

{ 
"_id" : 111, 
"section_tag" : "Section 1",
"sections" : [333, ...]
"blocks" : [1212, 2323, ...]
}

3. blocks 3. 块

{ 
"_id" : 1212, 
"block_type" : "paragraph" (or image etc.),
"content" : "<p> content goes here...</p>"
"conditions" : [condition_id_1, condition_id_2, ...] (these are also embedded documents)
}

So first of all I have to clone the document named 'page' and then I would continue cloning the 'sections' property of this document.因此,首先我必须克隆名为“page”的文档,然后继续克隆该文档的“sections”属性。 But for each 'section' document we also have other embedded documents and so on.但是对于每个“部分”文档,我们还有其他嵌入文档等等。

Is there any solution to this problem?这个问题有什么解决办法吗? I am concerned that in NoSQL databases it isn't a good idea having a tree model structure and collections being in relationships with others, would make it worse querying and also storing data.我担心在 NoSQL 数据库中,拥有树 model 结构和 collections 与他人建立关系并不是一个好主意,这会使查询和存储数据变得更糟。 Also I know about embedding the whole document and not only the Id of the documents it refers to, but there is a limit for the document size in MongoDb which is 16mb and this is not something I strive for.我也知道嵌入整个文档,不仅是它所引用的文档的 ID,而且 MongoDb 中的文档大小有一个限制,即 16mb,这不是我努力的目标。

Thank you for reaching this far and I hope you have understood the problem description.感谢您走到这一步,我希望您已经理解了问题描述。 I also apologize for my bad english.我也为我糟糕的英语道歉。

----Edited So after the cloning of a document, that is in the page collection, the database should look like: ----已编辑 所以在克隆文档后,即在页面集合中,数据库应该如下所示:

1. page collection 1.页面集合

{ 
"_id" : 4567890, 
"page_title" : "Title 1",
"sections" : [444, 555, ...]
"variables" : [v3, v4, ...]
}

2. sections collection (note how this document can reference to other documents of the same collection) 2.sections集合(注意这个文档如何引用同一集合的其他文档)

{ 
"_id" : 444, 
"section_tag" : "Section 1",
"sections" : [666, ...]
"blocks" : [3434, 4545, ...]
}

3. blocks 3. 块

{ 
"_id" : 3434, 
"block_type" : "paragraph" (or image etc.),
"content" : "<p> content goes here...</p>"
"conditions" : [condition_id_3, condition_id_4, ...] (these are also embedded documents ID's)
}

if you're talking about deep cloning a c# poco, you can use the following extension method for that:如果您正在谈论深度克隆 c# poco,您可以使用以下扩展方法:

public static class Extensions
{
    public static T Duplicate<T>(this T source)
    {
        return BsonSerializer.Deserialize<Container<T>>(
            new Container<T> { Data = source }.ToBson()
            ).Data;
    }

    private class Container<T>
    {
        public T Data { get; set; }
    }
}

and you can use it like so:你可以像这样使用它:

Page copy = page.Duplicate();

you will need the following imports:您将需要以下导入:

using MongoDB.Bson;
using MongoDB.Bson.Serialization;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM