开发者

Modeling Question for RavenDB (Or other doc oriented databases)

开发者 https://www.devze.com 2023-03-10 13:17 出处:网络
Wondering how some of the more experienced (or anyone with a better idea than I have) would tackle my particular modeling scenario...

Wondering how some of the more experienced (or anyone with a better idea than I have) would tackle my particular modeling scenario...

I have a typical "Category -> SubCategory ->TertiarySubCategory" scenario and I'm not sure if I'm mapping it out correctly. I am mapping this directly to an MVC route since raven seems to lend itself well with this. Under the final category (which can be at the first, 2nd or 3rd levels there will be a list of items associated with only that level of a category. So, we might have something like:

Single level category: '/Politics/'

Second level category: 'Politics/People' or 'Politics/Websites'

Tri-Level Category: 'Sports/Pro/Volleyball' or 'Sports/College/Football'

In a traditional RDBMS this is easy through primary/foreign keys + a few joins... so, wondering how I would handle with Raven?

From what I have read should I store the entire 'sports/pro/volleyball' URI or Key in a list of items that fall under it?

i.e. -

public class CategoryItem
{
     public string FriendlyName {get;set;} // Volleyball or Pro Volleyball
     public string CategoryURI {get;set;}  // i.e. - "/sports/pro/volleyball/"
     public string content {get;set;}  // i.e. - "Who is the best Pro Volleyball Athlete?"
     public List<string> Comments {get;set;}
}

// then we could store something like this:

var survey1 = new CategoryItem();
survey1.CategoryURI = "/sports/pro/volleyball/"
survey1.Content = "Who is the best female pro volleyball player?";
survey1.Comments.Add(new Comment("Misty May"));

var survey2 = new CategoryItem();
survey2.CategoryURI = "/sports/pro/volleyball/";
survey2.Content = "Who is the best male pro volleyball player?";
survey2.Comments.Add(new Comment("Some guy I don't kow");

// asuumes ravenSession was alreadyopened...开发者_JAVA百科 
ravenSession.Store(survey1);
ravenSession.Store(survey2);
ravenSessoin.SaveChanges();


//{ ...... etc .....  }
//Then I can query by CategoryURI without needing joins (denormalization)....  i.e. - 

var items = session.Query<CategoryItem>()
                 .Where(x => x.CategoryURI == "/sports/pro/volleyball/");

Or should I create a List items member of the actual category class? Each item would have a list of it's own comments... meaning everything's stored in a single document within Raven - i.e. -

public class Category
{
    public string FriendlyName {get;set;} // i.e. - "Volleyball" or "Pro Volleyball"
    public string URI {get;set;}  // i.e. -  "/sports/pro/volleyball"  which is the MVC path
    public List<CategoryItem> Items {get;set;}
}

public class CategoryItem
{
     public string Content {get;set;}
     public List<string> Comments {get;set;}
}

var vballCat = new Category();
vballCat.FriendlyName = "Pro Volleyball";
vballCat.URI = "/sports/pro/volleyball/";  // equivalent to the MVC route

var catItem = new CategoryItem().
catItem.Content = "Who is the best male pro volleyball player?";
catItem.Comments.Add("Misty May");
catItem.Comments.Add("Some Guy 1");
vballCat.Items.Add(catItem);

ravenSession.Store(vballCat);
ravenSession.SaveChanges();

..... now once I pull the primary cat i.e. - "/sports/pro/volleyball/" I have everything I need already under it

var items = session.Query<Category>()
                 .Where(x => x.URI == "/sports/pro/volleyball/");

{ ............. etc ............... }

Now here I can just iterate through the Items collection and it's collection of comments.... does this use eager loading? What if I had a million comments under one category item? When I load the main category would it load all one million comments too!?!?

I would appreciate any help you can provide. Sorry if this example/question is unclear... I'll try to clarify anything if you guys need it. Thanks again!


The answer is that it depends on the size of your data and your usage scenario. The first example is useful if you have large number of items and want to access categories without its items. The second example is useful if you usually access category with its items, and the size of items is limited (note that limited is still high, several thousands wouldn't cause me to blink). Note that there is no such thing as eager / lazy loading in RavenDB, you are talking about a single document vs. multiple documents, not about relations between documents. The entire document is loaded when you need it.

Another thing to remember is that it is usually faster to query by id than querying. That means that if you have ids that already looks very much like Document Ids, you might as well MAKE them the document ids.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号