Skip to content

Instantly share code, notes, and snippets.

@osmangoninahid
Created November 5, 2017 06:20
Show Gist options
  • Save osmangoninahid/e1442a9bf261df1ca94364b4a7379ed8 to your computer and use it in GitHub Desktop.
Save osmangoninahid/e1442a9bf261df1ca94364b4a7379ed8 to your computer and use it in GitHub Desktop.
Azure CosMos DB API for MongoDB New Released Aggregation queries support
/*
A sample schema
@Author : Osman Goni Nahid
{
FIELD1 : 1,
Bib : "12",
Name : "hummy Cummy",
Age : 24,
"M" : { "F" : "M"},
City : Portland,
State : "OR",
Country : "USA",
Citizen : "",
FIELD10 : "",
"5K" : "0:12:00",
"10K" : "0:12:00",
"15K" : "0:12:00",
"20K" : "0:12:00",
Half : "2:44:00",
25K : "2:00:00",
30K : "2:00:00",
35K : "2:00:00",
40K : "2:00:00",
40K : "2:00:00",
Pace : "2:00:00",
Proj Time : "-",
Official Time : "2:59:00",
Overall : 2,
Gender : 2,
Division : 2
}
*/
//simple match to find all data associated with the 'hummy cummy' in the dataset
Model.aggregate([{$match : {"Name":"Hummy Cummy"}}]);
//find all the runners that run faster than 3 hours
Model.aggregate([{$match : {"Official Time": {$lt : "3:00:00"}}}])
//Now we want to group those runners by country and see what the average place
//of the runners in each of thsese group is
//First execute the group stage :
Model.aggregate([{$match : {"Official Time": {$lt : "3:00:00"}}},{$group : {_id : "$Country"}}])
//Find the number of the runners in each of these groups
//We can use accumulator here
Model.aggregate([{$match : {"Official Time": {$lt : "3:00:00"}}}, {$group : {_id : "$Country", count : {$sum : 1}}}])
// Find the average overall place of each of the countries and put them in an order such that we can easily
// see the country with the minimum average
Model.aggregate([{$match : {"Official Time": {$lt : "3:00:00"}}}, {$group : {_id : "$Country", count : {$sum : 1}, avgPlace : {$avg : "Overall"}}}, {$sort : {"avgPlace":-1}}])
// But let's see who actually had the most runner that finished under this time
// Sory by count this time
Model.aggregate([{$match : {"Official Time": {$lt : "3:00:00"}}}, {$group : {_id : "$Country", count : {$sum : 1}, avgPlace : {$avg : "Overall"}}}, {$sort : {"count":1}}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment