RavenDB: how to make a simple map / reduce aggregation

Well, considering the set of such documents:

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

How to perform a simple unit at the prices listed? I do not want to do any grouping, just a scalar result.

i.e. sum, avg, etc.

I tried both of the following without success:

Card 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) }
+3
source share
2 answers

That should do the trick.

Map

from doc in docs.Products
select new { Name = "Total", Total = doc.Price }

Decrease:

select new
{
    Name = g.Key,
    Total = g.Sum(x => (double)x.Total)
}

Each document passes through a card, and aggregation occurs in the "Abbreviation". But the circuit coming out of the Map should still match your result. The “total” for each document is only its Price. All of them are then summarized in abbreviation.

Name , , -, .

+3

.

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

:

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

, , . , :

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

JSON :

{"Results":[{"Name":"Total","Price":"14","Price_Range":"14"}],"IsStale":false,"TotalResults":1}

, . RavenDB, , , , Sum() .

0

Source: https://habr.com/ru/post/1756542/


All Articles