I have something like this in RavenDb:
Path [hasMany] Goals [hasMany] Achievements
So I have one document that contains goals and achievements.
If it where a traditional database, I could make this query
select g.id, count(a.id)
from path p
join goals g on p.id = g.pathId
join achievement a on a.id = g.id
group by goal.id
How do I do this query in ravendb?
Right now I’m doing it like this:
Path path = RavenSession.Load<Path>(pathId);
path.Goals.Select(x => new
{
Id = x.Id,
Name = x.Name,
TOnCourse = x.Achievements.Where(y => y.Resolution == Resolution.OnCourse).Count(),
TAstray = x.Achievements.Where(y => y.Resolution == Resolution.Astray).Count()
});
I don’t want to do it this way, because I think that I’m loading all the “Goals” and “Achievements” and what I only want is to make the aggregation in the DB (ravendb)
If you want to know what Path, Goals and Achievements really are in my model here is my “pet” project http://dpath.apphb.com/
EDIT
I think I will get those “calculated” values and put them in Goal as properties, because I think the calculation is going to be very common, so keeping it simple since an achievement cannot be deleted, and the user will have a “relation” with goal and the quantity of achievements in order to get “My Stats”
Nevertheless, I would like to know how would you accomplish that query in RavenDb
Thanks!
You are loading just a single document for this, so that is likely to not be an issue.
You aren’t loading anything else.
You can do this in an index, and then load the stored fields from there, but I don’t think you need this unless your document is very large.