Question

I am just starting to learn about mongo db and was wondering if I am doing something wrong....I have two objects:

public class Part 
{
    public Guid Id;
    public ILIst<Materials> Materials;
}

public class Material
{
   public Guid MaterialId;
   public Material ParentMaterial;
   public IList<Material> ChildMaterials;
   public string Name;
}

When I try to save this particular object graph I receive a stack overflow error because of the circular reference. My question is, is there a way around this? In WCF I am able to add the "IsReference" attribute on the datacontract to true and it serializes just fine.

Was it helpful?

Solution 4

I was able to accomplish exactly what I needed by using a modified driver from NoRM mongodb.

OTHER TIPS

What driver are you using?

In NoRM you can create a DbReference like so

public DbReference<Material> ParentMaterial;

Mongodb-csharp does not offer strongly typed DbReferences, but you can still use them.

public DBRef ParentMaterial;

You can follow the reference with Database.FollowReference(ParentMaterial).

Just for future reference, things like references between objects which are not embedded within a sub-document structure, are handled extremely well by a NoSQL ODB, which is generally designed to deal with transparent relations in arbitrarity complex object models.

If you are familiar with Hibernate, imagine that without any mapping file AT ALL and orders of magnitude faster performance because there is no runtime JOIN behind the scenes, all relations are resolved with the speed of a b-tree lookup.

Here is a video from Versant (disclosure - I work for them), so you can see how it works.

This is a little boring in the beginning, but shows every single step to take a Java application and make it persistent in an ODB... then make it fault tolerant, distributed, do some parallel queries, optimize cache load, etc...

If you want to skip to the cool part, jump about 20 minutes in and you will avoid the building of the application and just see the how easy it is to dynamically evolve schema, add distribution and fault tolerance to any existing application ):

If you want to store object graphs with relationships between them requiring multiple 'joins' to get to the answer you are probably better off with a SQL-style database. The document-centric approach of MongoDB and others would probably structure this rather differently.

Take a look at MongoDB nested sets which suggests some ways to represent data like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top