Re: [RavenDB] Seprating post Comments from post

96 views
Skip to first unread message

Oren Eini (Ayende Rahien)

unread,
Dec 31, 2012, 3:25:14 AM12/31/12
to rav...@googlegroups.com
There are times when we want to show the blog posts without comments (basically any time that they are in a list).
It makes things slightly more efficeint.

On Mon, Dec 31, 2012 at 10:18 AM, payam yazdkhasti <yazdk...@gmail.com> wrote:
In Roccon Blog there is class for post (Post class) and another class for post comments (PostComments). What is the reason for seprating blog post from it's comments?

Khalid Abuhakmeh

unread,
Dec 31, 2012, 2:35:38 PM12/31/12
to rav...@googlegroups.com
You might have a post go viral and get 10,000 comments. You might not want to pull 10,000 comments every time you display the page, but instead you might want to show the last 20 comments and then page.

When modeling RavenDB documents you have to take time to understand the relationships between your objects. I usually start by talking to myself out loud about my domain and see what makes sense.

payam yazdkhasti

unread,
Jan 1, 2013, 12:24:42 AM1/1/13
to rav...@googlegroups.com

I created an index with TransformResults function and queried a post with its top x comments using that index when storing post with its comments in one document.

How can i query a post and its last 20 comments when storing comments in a seprate document?

Payam Yazdkhasti

unread,
Jan 1, 2013, 9:22:18 AM1/1/13
to rav...@googlegroups.com
I can query a post with its recent comments like this:


 public class BlogPost
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int ViewCount { get; set; }
        public string BlogPostCommentCollectionId { get; set; }
    }

public class IBlogPostWithRecentCommentsQueryResult
    {
        public BlogPost Post { get; set; }
        public ICollection<Comment> RecentComments { get; set; }
    }

 public class Comment
    {
        public int Id { get; set; }
        public string Content { get; set; }
        public DateTime CreatedOn { get; set; }
    }

 public class IBlogPostQueryResult
    {
        public BlogPost Post { get; set; }
        public ICollection<Comment> RecentComments { get; set; }
    }

public class GetPostWithTopRelatedComments : AbstractIndexCreationTask<BlogPost>
    {
        public GetPostWithTopRelatedComments()
        {

            Map = posts => from post in posts
                           select new {post.Id, post.Title };

            TransformResults = (database, posts) => from post in posts
                                                    let CommentCollection = database.Load<BlogPostCommentCollection>(post.BlogPostCommentCollectionId)
                                                    select new { post, RecentComments = CommentCollection.Comments.OrderBy(c => c.CreatedOn).Take(10) };
        }
    }


I tested this code and it worked. Is there a better solution?

Oren Eini (Ayende Rahien)

unread,
Jan 1, 2013, 9:30:00 AM1/1/13
to rav...@googlegroups.com
Why not simply load the entire comments doc? that is the simplest and most performant way

Payam Yazdkhasti

unread,
Jan 1, 2013, 9:34:02 AM1/1/13
to rav...@googlegroups.com
Sorry, my code is:
 public class BlogPost
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int ViewCount { get; set; }
        public string BlogPostCommentCollectionId { get; set; }
    }

 public  class BlogPostCommentCollection
    {
      public string Id { get; set; }
      public string BlogPostId { get; set; }
      public ICollection<Comment> Comments { get; set; }
    }

 public class Comment
    {
        public int Id { get; set; }
        public string Content { get; set; }
        public DateTime CreatedOn { get; set; }
    }

public class IBlogPostWithRecentCommentsQueryResult
    {
        public BlogPost Post { get; set; }
        public ICollection<Comment> RecentComments { get; set; }
    }

Oren Eini (Ayende Rahien)

unread,
Jan 1, 2013, 9:36:54 AM1/1/13
to rav...@googlegroups.com
Why not simply load the entire comments doc? that is the simplest and most performant way

Payam Yazdkhasti

unread,
Jan 1, 2013, 9:47:31 AM1/1/13
to rav...@googlegroups.com
Is it good to load the entire comments doc If a post has 1000 or more comments?

Oren Eini (Ayende Rahien)

unread,
Jan 1, 2013, 9:51:00 AM1/1/13
to rav...@googlegroups.com

Carlos Mendes

unread,
Jan 3, 2013, 8:31:03 PM1/3/13
to rav...@googlegroups.com
I like the auctions model but how can we deal with operations on individual comments (eg.: delete, report as spam/abusive, rate...)?

Oren Eini (Ayende Rahien)

unread,
Jan 4, 2013, 3:47:03 AM1/4/13
to ravendb

Carlos Mendes

unread,
Jan 4, 2013, 6:32:19 AM1/4/13
to rav...@googlegroups.com
Got it. Thks
Reply all
Reply to author
Forward
0 new messages