Azure Cosmos DB - Mongo API是否支持聚合操作?

8

我想使用cosmosdb存储按照文档中心设计排列的时间序列数据。使用官方mongodb驱动程序,我尝试编写一些简单的聚合语句;然而,我收到了错误消息,指出$unwind和$group不受支持。考虑到cosmosdb被吹捧为mongodb的即插即用替代品,我错过了什么步骤,还是聚合不是一个受支持的功能。

retrieve.Aggregate().Unwind(i => i.Observations).Group(new BsonDocument {
        {"_id", "$Observations.Success"},
        {"avg", new BsonDocument{
        {"$avg", "$Observations.Duration"}
        }}
    }).ToList();

考虑到底层实现是将“驱动程序”发出的线路协议“映射”到特定的CosmosDB方法,因此这些特定的错误表明不支持该功能。快速搜索引擎显示最近添加了“SQL聚合”功能,但没有任何文档表明支持MongoDB聚合。因此,除非最近才添加了支持,否则缺乏文档以及错误加强了这种情况,意味着“不支持”。我认为具体的错误实际上是关键。 - Neil Lunn
@NeilLunn 我也这么怀疑。也许我太天真了,但我希望只是我做错了什么。 - Tedford
@Tedford - 现在支持聚合管道。请查看我的相关答案(以及支持功能列表的发布链接)。 - David Makogon
2个回答

8

根据@neillunn的评论和缺乏任何相关文档,似乎cosmosdb的聚合函数不支持mongo API。看起来意图是使用cosmosdb Documentdb API SQL语法进行聚合。

LINQ语法

var client = new DocumentClient(new Uri("https://<account>.documents.azure.com"),<password>);

    // issue query
    var documentUri = UriFactory.CreateDocumentCollectionUri("Prod", "retrieve");
    var date = "20161011".Dump("Date");


    var max = client.CreateDocumentQuery<ObservationDocument>(documentUri)
          .Where(i => i.Id == date)
          .SelectMany(i => i.observations)
          .Max(i => i.Duration);

SQL语法

// explicit query
var spec = new SqlQuerySpec("select value count(o.duration) from days d join o in d.observations where d._id = @id");
spec.Parameters = new Microsoft.Azure.Documents.SqlParameterCollection();
spec.Parameters.Add(new Microsoft.Azure.Documents.SqlParameter("@id", "20161010"));
client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("Prod", "retrieve"), spec).Dump("As query");

4

截至2017年11月,Cosmos DB的MongoDB API现在支持聚合管道,包括您原始问题中识别的所有管道阶段($unwind$group)。

支持功能的具体细节在这里列出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接