简体   繁体   English

MongoDB C# 驱动程序 - POCO 引用的序列化?

[英]MongoDB C# driver - serialization of POCO references?

I'm researching MongoDB at the moment.我目前正在研究MongoDB It's my understanding that the official C# driver can perform serialization and deserialization of POCOs .据我了解,官方的C#驱动可以进行 POCO 的序列化和反序列化。 What I haven't found information on yet is how a reference between two objects is serialized.我还没有找到关于如何序列化两个对象之间的引用的信息。 [I'm talking about something that would be represented as two seperate documents, with ID links, rather than embeded documents. [我说的是可以表示为两个单独的文档的东西,带有 ID 链接,而不是嵌入文档。

Can the serialization mechanism handle this kind of situation?序列化机制能处理这种情况吗? (1): (1):

class Thing {
    Guid Id {get; set;}
    string Name {get; set;}
    Thing RelatedThing {get; set;}
}

Or do we have to sacrifice some OOP, and do something like this?还是我们必须牺牲一些OOP,做这样的事情? (2): (2):

class Thing {
    Guid Id {get; set;}
    string Name {get; set;}
    Guid RelatedThing_ID {get; set;}
}

UPDATE:更新:

Just a couple of related questions then...那么只有几个相关的问题......

a) If the serializer is able to handle situation (1). a)如果序列化器能够处理情况(1)。 What is an example of how to do this without using embedding?在不使用嵌入的情况下如何做到这一点的例子是什么?

b) If using embedding, would it be possible to query across all 'Things' regardless of whether they were 'parents' or embedded elements? b)如果使用嵌入,是否可以查询所有“事物”,无论它们是“父母”还是嵌入元素? How would such a query look like?这样的查询会是什么样子?

The C# driver can handle serializing the class containing a reference to another instance of itself (1). C# 驱动程序可以处理序列化 class 包含对自身另一个实例 (1) 的引用。 However:然而:

  1. As you surmised, it will use embedding to represent this如您所料,它将使用嵌入来表示
  2. There must be no circular paths in the object graph or a stack overflow will occur object 图中不能有循环路径,否则会发生堆栈溢出

If you want to store it as separate documents you will have to use your second class (2) and do multiple inserts.如果要将其存储为单独的文档,则必须使用第二个 class (2) 并进行多次插入。

Querying across multiple levels is not really possible when the object is stored as one large document with nested embedding.当 object 存储为一个具有嵌套嵌入的大型文档时,实际上不可能跨多个级别进行查询。 You might want to look at some alternatives like:您可能想查看一些替代方案,例如:

https://docs.mongodb.com/manual/applications/data-models-tree-structures/ https://docs.mongodb.com/manual/applications/data-models-tree-structures/

Yes, That is completely possible.是的,这是完全可能的。

One thing you must understand about MongoDB and most NoSQL solutions is that objects can be contained within other objects.关于 MongoDB 和大多数 NoSQL 解决方案,您必须了解的一件事是对象可以包含在其他对象中。 In the case of MongoDB, it's basically, if you can create the object in JSON, then you can create the object in MongoDB. In the case of MongoDB, it's basically, if you can create the object in JSON, then you can create the object in MongoDB.

In general, you should strive to have a "relatively" denormalized database structure.一般来说,您应该努力拥有一个“相对”非规范化的数据库结构。 A little bit of duplicated data is ok as long as you're not updating it often.只要您不经常更新它,一点点重复的数据就可以了。

I've encountered the same issue recently, and I usually steer away from them but... I'm thinking that this could be a good use for a significant numbering system deployed on the Id field.我最近遇到了同样的问题,我通常会避开它们,但是......我认为这对于部署在 Id 字段上的重要编号系统可能是一个很好的用途。

class Thing {
string Id {get; set;}
string Name {get; set;}
string RelatedThing {get; set;}}

So, simplifying, if Id was something like "T00001" (or indeed T + GUID), you could easily get the set of things from Mongo by querying for something like Id starts with T , and setting up objects for them all (or just for the subset you know contains your reference, if it is a very large set).因此,简化一下,如果Id类似于“T00001”(或者实际上是 T + GUID),您可以通过查询以 T 开头的 Id之类的东西,并为它们设置对象(或只是对于您知道的包含您的参考的子集,如果它是一个非常大的集合)。

You know/expect that RelatedThing to be a Thing , but it will just be a string when it comes back from Mongo.你知道/期望RelatedThing是一个Thing ,但是当它从 Mongo 返回时它只是一个字符串。 But if you've set up objects as above, you could effectively use the string as if it were an object reference (after all, that is what it really is, done kind of "manually").但是,如果您已按上述方式设置对象,则可以像使用 object 引用一样有效地使用该字符串(毕竟,这就是它的真正含义,是“手动”完成的)。

Its a 'loose' way of doing it, but might be workable for you.它是一种“松散”的方式,但可能对您有用。

Can anyone see any pitfalls with that approach?任何人都可以看到这种方法的任何陷阱吗?

If you really want a reference to another document, you can use a DBRef .如果你真的想引用另一个文档,你可以使用DBRef However there is limitation with references in MongoDB.但是 MongoDB 中的参考文献存在限制。

  • you can only query by id on a ref您只能在 ref 上通过 id 查询
  • when you get your Thing's document, you'll have to make a second query to get the associated RelatingThing's document as join doesn't exists in MongoDB.当您获得 Thing 的文档时,您必须进行第二次查询以获取关联的 RelatingThing 的文档,因为 MongoDB 中不存在连接。

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

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