tags:

views:

249

answers:

2

Ok given a collection of documents like this:

{ 
  "Name": "A",
  "Price": 1.50
}

How do I perform a simple aggregation on the prices in the list? I don't want to perform any grouping, just a simple scalar result.

ie. sum, avg etc

I have tried both of the following without success:

Map only:

for doc in docs
select new { sumPrices = Sum(d => d.Price) }

and

Map:

for doc in docs
select new { price = d.Price }

Reduce:

for result in results
select new { sumPrices = Sum(result) }
A: 

Here's what I came up with.

Map: from doc in docs where doc["@metadata"]["Raven-Entity-Name"] == "Books" select new { Name = "Total", Price = doc.Price };

Reduce: from result in results group result by result.Name into g select new { Name = g.Key, Price = g.Sum(x => x.Price) }

However, I was only able to get this to work if I changed the price to an integer. So my two docs look like this:

{
    "Name":"B",
    "Price":4
}
{
    "Name":"A",
    "Price":10
}

And my JSON output looks like this: {"Results":[{"Name":"Total","Price":"14","Price_Range":"14"}],"IsStale":false,"TotalResults":1}

Obviously, changing the price to integers is not the solution. It may be a bug in RavenDB, but I'm pretty new to it myself, so I'm hoping it's just the way I'm doing my Sum() or the way that the data is represented in the document.

Pete Nelson
A: 

Why did you have to change the price to int?

Ayende Rahien
Changing any of the prices to a decimal gets me this result: {"Results":[],"IsStale":false,"TotalResults":0}I was only able to get my map/reduce to work if I changed the prices to ints.
Pete Nelson