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();
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.
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With