Hello all,
I think I hit a bit of non-intuitive behaviour for ravendb and I'd like some help in figuring it out.
When I store a document and create an index with counts for an given inner property of that document, the counts add up whenever that document is changed, instead of calculating properly.
The model is below at [1], and the index is at [2]. Project instances are entirely saved as an aggregate root, and I index over tasks per user to get a count.
I understand that it might make sense to store each entity separately, but I was building this as a sample for a presentation to guide some discussion around different ways of modelling data and it might make sense on some domains. Also, my impression is that this would also cause issues with models like tag-counts...
I've created a console application using an in-memory store that can serve as a test with build 910 (and I've caught this also on 888). It's up at
https://gist.github.com/2625592 .
I've also tried de-normalizing the owner into ownerid and ownername in case that was the issue, bit the error remains.
My feeling is that it's a bug, but I'm not positive.
Ideas?
[1]-
public class Project
{
public string Id { get; set; }
public string Name { get; set; }
public IList<Activity> Activities { get; set; }
public Project()
{
Activities = new List<Activity>();
}
}
public class Activity
{
public string Name { get; set; }
public Person Owner { get; set; }
public IList<Task> Tasks { get; set; }
public Activity()
{
Tasks = new List<Task>();
}
}
public class Task
{
public string Name { get; set; }
public Person Owner { get; set; }
public bool Done { get; set; }
}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
[2]-
public class TasksCount_ForPerson : AbstractIndexCreationTask<Project, TasksCount_ForPerson.Result>
{
public class Result
{
public Person Owner { get; set; }
public int Count { get; set; }
}
public TasksCount_ForPerson()
{
Map = projects =>
projects
.SelectMany(p => p.Activities)
.SelectMany(a => a.Tasks)
.Select(task =>
new
{
task.Owner,
Count = 1
}
);
Reduce =
results => results
.Select(p => new
{
Owner = p.Select(r => r.Owner).First(),
Count = p.Sum(result => result.Count)
});
}
}