Question

I'm using MongoRepository and would like to create references between two different collections.

For example, A form could have a link to a form. It would be represented like:

public class Form : IEntity{
     public string Id {get;set;}
     public string Report {get;set;} // represents the string form of the ObjectId
}

public class Report : IEntity{
     public string Id {get;set;}
}

I want to fetch the Form document with the Report nested like:

static MongoRepository<Form> forms = new MongoRepository<Form>();
var form = forms.Single(f => f.id == "1");

and the result look like:

{
    "id": "1",
    "Report": {
        "id": "2"
    }
}

Is this possible inside this framework? Is it possible just using the C# Driver base?

Was it helpful?

Solution

Don't know much about MongoRepository but in the official driver you can:

  1. have a document that holds an ID to a different document in another collection (basically a foreign key) but that means you have to do a "join on client side", selecting the first item and using the ID selecting the other one.
  2. You can store the second item (Report in this case) completely inside the first one. That means it's an inner document that sits on the same collection as it's parent and you can select both of them together in a single call.

MongoRepository probably doesn't add much to this. It can only abstract that logic away from you...

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