2017-06-13 227 views
7

我正在寻找使用cosmosdb来存储时间序列数据的document-centric design。使用官方的mongodb驱动程序,我尝试写一些简单的聚合语句;但是,我收到错误,指出$ unwind和$ group不受支持。鉴于cosmosdb被吹捧为对mongodb的一种直接替代,我错过了一个步骤或者聚合不是支持的功能。Azure Cosmos DB-Mongo API支持汇总

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

考虑到“底下”会发生什么,从“驱动程序”发出的有线协议被“映射”到特定的CosmosDB方法,那么这些特定的错误将表明它不被支持。一小段“搜索引擎fu”只显示了最近添加的内容,允许“SQL聚合”,并且根本没有任何文档表明对MongoDB聚合有任何支持。因此,除非最近添加,否则没有任何文件加上错误来加强案件意味着“否”。我认为具体的错误实际上是这里的具体事情。 –

+0

@NeilLunn我也怀疑这一点。叫我天真,但我希望我做错了什么。 – Tedford

+0

@Tedford - 现在支持聚合管道。查看我的相关答案(连同发布的支持功能列表的链接)。 –

回答

2

截至11月2017,宇宙DB的MongoDB的API现在支持聚合管道,包括所有在你原来确定的管线级问题($unwind,$group)。

支持功能的细节列表here

7

鉴于从@neillunn注释和缺乏对影响是似乎对cosmosdb聚合功能不通过蒙戈API支持的任何文件。看起来intention是为集合使用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");