按DocumentDB中的字段分组
是否有可能以某种方式对DocumentDB中的字段进行分组,是否存储过程?
假设我有以下集合:
[ { name: "Item A", priority: 1 }, { name: "Item B", priority: 2 }, { name: "Item C", priority: 2 }, { name: "Item D", priority: 1 } ]
我想获得最高优先级组中的所有项目(在这种情况下优先级为2)。 我不知道最优先的是什么价值。 即:
[ { name: "Item B", priority: 2 }, { name: "Item C", priority: 2 } ]
使用一些粗略的LINQ,它看起来像这样:
var highestPriority = collection .GroupBy(x => x.Priority) .OrderByDescending(x => x.Key) .First();
DocumentDB目前不支持GROUP BY或任何其他聚合。 它是第二个最需要的function,在DocumentDB UserVoice上列为“正在审核”。
同时, documentdb-lumenize是作为存储过程编写的DocumentDB的聚合库。 您将cube.string
作为存储过程加载,然后使用聚合配置调用它。 这个例子有点矫枉过正,但它完全有能力做你在这里问的问题。 如果将其传递给存储过程:
{cubeConfig: {groupBy: "name", field: "priority", f: "max"}}
应该做你想要的。
注意,Lumenize可以做很多事情,包括简单的group-by和其他函数(sum,count,min,max,median,p75等),数据透视表,以及一直到复杂的n维超立方体每个单元格多个指标。
我从未尝试过从.NET加载cube.string,因为我们在node.js上,但是它是以字符串而不是javascript的forms提供的,因此您可以轻松加载和发送它。
或者,您可以编写存储过程来执行此简单聚合。
DocumentDB中仍然不支持GroupBy,上面介绍了最好的方法(使用存储过程),或者如上所述的UserVoice项中使用Spark连接器。 但是,如果要分组的集合相对较小,还有另一种解决方案:
获取所有结果,无需从集合中进行分组,并在内存中执行分组。
因此,而不是:
var highestPriority = collection .GroupBy(x => x.Priority) .OrderByDescending(x => x.Key) .First();
你用:
var highestPriority = collection .Where() .AsEnumerable() .GroupBy(x => x.Priority) .OrderByDescending(x => x.Key) .First();
.AsEnumerable()从documentDB获取结果,然后在内存中完成groupBy。 但请注意,这不是最佳解决方案,只应在您确定结果集较小的情况下使用。
- Azure Native Application通用身份validation
- 将最多100,000条记录插入DocumentDB的最快方法
- 不接受Azurefunction(ILogger或TraceWriter)的上次日志记录参数
- Stackexchange.Redis超时和socketfailures
- 在webjob sdk代码fork之后为公共类ServiceBusTriggerAttribute获取exception
- 如何在Azure应用程序洞察中忽略localhost
- 使用C#反序列化Avro文件
- 尝试连接到azure数据库突然停止工作,抛出exception
- Windows azure REST API列出容器问题