Grouping by field in DocumentDB

Is it possible to somehow group a field in DocumentDB, a stored procedure or not?

Let's say I have the following collection:

[ { name: "Item A", priority: 1 }, { name: "Item B", priority: 2 }, { name: "Item C", priority: 2 }, { name: "Item D", priority: 1 } ] 

I would like to get all the items in the group with the highest priority (priority 2 in this case). I do not know what the highest priority matters. I.e:.

 [ { name: "Item B", priority: 2 }, { name: "Item C", priority: 2 } ] 

With some rough LINQ, it would look something like this:

 var highestPriority = collection .GroupBy(x => x.Priority) .OrderByDescending(x => x.Key) .First(); 
+2
source share
1 answer

DocumentDB does not currently support GROUP BY or any other aggregation. This is the second most requested feature and is listed as a "Browse" in DocumentDB DocumentDB .

At the same time, documentdb-lumenize is an aggregation library for DocumentDB written as a stored procedure. You load cube.string as a stored procedure, then call it with the aggregation configuration. This is a bit overkill for this example, but it is perfectly capable of doing what you ask for here. If you pass this to a stored procedure:

 {cubeConfig: {groupBy: "name", field: "priority", f: "max"}} 

which should do what you want.

Note. Lumenize can do much more than simple, with a simple group function (with a common function (sum, count, min, max, median, p75, etc.), pivot tables and even complex n-dimensional hypercubes with several metrics on cell.

I have never tried loading cube.string from .NET because we are on node.js, but it is sent as a string, not javascript, so you can easily download and send it.

Alternatively, you can write a stored procedure for this simple aggregation.

+4
source

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


All Articles