Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 716
Tedford Avatar asked Jun 13 '17 22:06

Tedford


People also ask

Is Cosmos DB compatible with MongoDB?

The Azure Cosmos DB's API for MongoDB is compatible with MongoDB server version 3.6 by default for new accounts. The supported operators and any limitations or exceptions are listed below. Any client driver that understands these protocols should be able to connect to Azure Cosmos DB's API for MongoDB.

Which API is supported by Azure Cosmos DB?

Azure Cosmos DB offers multiple database APIs, which include the Core (SQL) API, API for MongoDB, Cassandra API, Gremlin API, and Table API. By using these APIs, you can model real world data using documents, key-value, graph, and column-family data models.

What is Azure Cosmos DB API for MongoDB account?

Azure Cosmos DB API for MongoDB implements the wire protocol for MongoDB. This implementation allows transparent compatibility with native MongoDB client SDKs, drivers, and tools. Azure Cosmos DB does not host the MongoDB database engine.

How do I use Cosmos DB with MongoDB API?

On the New page, select Databases > Azure Cosmos DB. On the Select API option page, select Azure Cosmos DB API for MongoDB > Create. The API determines the type of account to create. Select Azure Cosmos DB API for MongoDB because you will create a collection that works with MongoDB in this quickstart.


Video Answer


1 Answers

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");
like image 123
Tedford Avatar answered Sep 21 '22 21:09

Tedford