Say I have a blog
with a collection of posts
, and each post
has a collection of comments
. I want to query all the comments to find the 5 most recent across all blog posts. With an RDBMS you simply go straight for the comments table, sort by date and take 5. Is it possible to do something similar within Ravendb, given that comments aren't the aggregate root?
highace, Yes, you can certainly do that. You can see how we do something similar here: https://github.com/ayende/RaccoonBlog/blob/master/RaccoonBlog.Web/Infrastructure/Indexes/PostComments_CreationDate.cs
It makes sense to store your comments as seperate documents rather than in each post otherwise you end up loading and saving the entire post document every time a comment is added.
With this in mind you would simply query your comments something like the following
session.Query<Comment>().OrderByDescending(x => x.CommentDate).Take(5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With