简体   繁体   中英

Does Azure Cosmos DB - Mongo API support aggregation

I was looking to use cosmosdb to store time series data laid out in a document-centric design . Using the official mongodb driver I tried to write some simple aggregation statements; however, I received errors stating that $unwind and $group were not supported. Given that cosmosdb was touted as a drop-in replacement to mongodb did I miss a step or is aggregation not a supported feature.

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

Given the comment from @neillunn and the lack of any documentation to that affect is seems that the aggregation functions for cosmosdb are not supported via the mongo API. It appears that the intention is to use the cosmosdb Documentdb API SQL syntax for aggregates.

LINQ Syntax

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 syntax

// 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");

As of November 2017, Cosmos DB's MongoDB API now supports aggregation pipeline, including all of the pipeline stages identified in your original question ( $unwind , $group ).

Specifics of supported features are listed here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM