C # - MongoDB driver serialization of POCO links?

I am studying MongoDB at the moment. I understand that the official C # driver can serialize and deserialize POCOs . I have not yet found information on how a link is ordered between two objects. [I am talking about something that will be presented in the form of two separate documents with identification links, and not for embedding documents.

Can the serialization engine handle this situation? (one):

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

Or should we sacrifice OOP and do something similar? (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 cope with the situation (1). What is an example of how to do this without using attachments?

b) If you are using an attachment, would it be possible to query all โ€œThingsโ€ regardless of whether they were โ€œparentsโ€ or embedded elements? What does this query look like?

+7
source share
4 answers

The C # driver can handle serialization of a class containing a link to another instance of itself (1). But:

  • As you might guess, he will use an attachment to represent this
  • There should not be circular paths in the graph of objects or the stack will overflow

If you want to save it as separate documents, you will have to use your second class (2) and make several inserts.

A request for several levels is not possible if the object is stored as one large document with an attached attachment. You can look at some alternatives, for example:

http://www.mongodb.org/display/DOCS/Trees+in+MongoDB

+11
source

Yes, it is quite possible.

One thing you should understand about MongoDB and most NoSQL solutions is that objects can be contained in other objects. In the case of MongoDB, this is basically if you can create an object in JSON, then you can create an object in MongoDB.

In general, you should strive for a "relatively" denormalized database structure. A little duplicate data is good if you do not update it often.

0
source

If you really need a link to another document, you can use DBRef . However, there is a limitation with references in MongoDB.

  • you can only request id from the link
  • when you receive your Thing document, you will need to make a second request to get the related RelatingThing document, since the connection does not exist in MongoDB.
0
source

I recently ran into the same problem, and I usually shy away from them, but ... I think this might be useful for a significant numbering system deployed in the Id field.

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

So, simplifying if Id was something like โ€œT00001โ€ (or even T + GUID), you could easily get a lot of things from Mongo by requesting something like Id starting with T and creating objects for them (or just for the subset that you know contains your link if it's a very large set).

You know / expect RelatedThing to be a thing, but it will just be a string when it returns from Mongo. But if you configured the objects as described above, you could effectively use this line as if it were a reference to the object (in the end, this is what actually happens, done โ€œmanuallyโ€).

His "free" way to do this, but may be useful to you.

Can anyone see any pitfalls with this approach?

0
source

All Articles