Hi all,
I am having some problems creating map/reduce indexes.
I have the following structure defining my DEBT documents:
public class Debt
{
public string DebtorName {get;set;}
public DateTime DueDate {get;set;}
public decimal Amount {get;set,}
}
I want to group all the debts by debtor name up to now. So basically, I want to know how much a debtor owes until now, adding up all the amounts of his debts that are due to be paid.
I have tried to create an index for that but I am not sure how to do it. I understand why I cannot use DateTime.Now in my index.
public class PresentDebts : AbstractIndexCreationTask<Debt>
{
public PresentDebts ()
{
Map = debts=> from debt in debts
select new
{
DebtorName = debt.DebtorName,
Amount = debt.Amount
};
Reduce = results => from result in results
group result by result.DebtorName
into g
select new
{
DebtorName = g.Key,
Amount = g.Sum(x => x.Amount)
};
}
}
How I use my index:
var debts = session.Query<Debt, PresentDebts>().Where(x => x.DueDate < DateTime.Now);
It returns 0 results. Is the "where" clause applied to the dataset being mapped in the index or afterwards on the grouped results ?
What is the best approach for this situation? Is it possible to do it with an index?
I have way more than 128 debts prior to today in my db, so I just wanted to avoid grabbing them all, and group them outside the db.
Thanks.
But I still need to group them all in one debt per debtor afterwards, and I don't know how to do it.
Modifying my index as you said grouping by debtor and debt date, and filtering by previous debts afterwards, I still get a collection of debts per debtor and what I want is a unique debt per debtor with all the amounts added up.
I would still need something like this but that is not supported because of the GroupBy. I wanted to avoid doing ToList() before the GroupBy.
var debts = session.Query<Debt, PresentDebts>().Where(x => x.DueDate < DateTime.Now).GroupBy(x => x.DebtorName).Select(g => new Debt{ DebtorName = g.Key, Amount = g.Sum(x => x.Amount});
Is it possible to do it without fetching them all from the db?