按DocumentDB中的字段分组 [英] Grouping by a field in DocumentDB

查看:40
本文介绍了按DocumentDB中的字段分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过某种方式对DocumentDB中的字段进行分组(存储过程是否可用)?

Is it possible, in some way, to group upon a field in DocumentDB, stored procedure or not?

假设我有以下收藏:

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

我想获得优先级最高的组中的所有项目(在这种情况下,优先级为2).我不知道最高优先级的值是多少.即:

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

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

使用一些粗糙的LINQ,看起来会像这样:

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

var highestPriority = 
    collection
        .GroupBy(x => x.Priority)
        .OrderByDescending(x => x.Key)
        .First();

推荐答案

DocumentDB当前不支持GROUP BY或任何其他聚合.它是第二个要求最多的功能,在 DocumentDB UserVoice 上被列为审核中".

DocumentDB currently does not support GROUP BY nor any other aggregation. It is the second most requested feature and is listed as "Under Review" on the DocumentDB UserVoice.

同时, documentdb-lumenize 是一个DocumentDB的聚集库,以存储方式编写程序.您将cube.string作为存储过程加载,然后使用聚合配置调用它.这个例子有点过分,但是完全有能力完成您在这里要问的事情.如果将其传递给存储过程:

In the mean time, documentdb-lumenize is an aggregation library for DocumentDB written as a stored procedure. You load cube.string as a stored procedure, then you call it with an aggregation configuration. It's a bit overkill for this example, but it's perfectly capable of doing what you are asking here. If you pass this into the stored procedure:

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

那应该做你想要的.

请注意,Lumenize的功能远不止于简单的分组方法以及其他功能(总和,计数,最小值,最大值,中位数,p75等),数据透视表,以及直至复杂的n-每个单元具有多个度量的三维超立方体.

Note, Lumenize can do a lot more than that including simple group-by's with other function (sum, count, min, max, median, p75, etc.), pivot tables, and all the way up to complicated n-dimensional hypercubes with multiple metrics per cell.

我从未尝试过从.NET加载cube.string,因为我们使用的是node.js,但是它以字符串而不是JavaScript的形式提供,因此您可以轻松地加载和发送它.

I have never tried loading cube.string from .NET because we're on node.js, but it is shipped as a string rather than javascript so you can easily load and send it.

或者,您可以编写一个存储过程来进行这种简单的聚合.

Alternatively, you could write a stored procedure to do this simple aggregation.

这篇关于按DocumentDB中的字段分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆