Hi @Laura_Logan,
Thanks for looking into this project and for this question !
That’s correct. In the REST API, you just have access to all the documents but I didn’t provide an API which provides a way to aggregate the data together… Because there are so many different aggregations we could build !
The easiest way to get this result is actually to go back to the MongoDB cluster directly and run an Aggregation Pipeline there directly.
You can connect like this:
mongo "mongodb+srv://readonly:readonly@covid-19.hip2i.mongodb.net/covid19"
Then you can use this aggregation for example:
[
{
'$group': {
'_id': '$state',
'cases': {
'$sum': '$confirmed_daily'
},
'deaths': {
'$sum': '$deaths_daily'
}
}
}
]
Which gives me the expected result in the Mongo Shell:
MongoDB Enterprise covid-19-shard-0:PRIMARY> use covid19
switched to db covid19
MongoDB Enterprise covid-19-shard-0:PRIMARY> db.us_only.aggregate([
... {
... '$group': {
... '_id': '$state',
... 'cases': {
... '$sum': '$confirmed_daily'
... },
... 'deaths': {
... '$sum': '$deaths_daily'
... }
... }
... }
... ])
{ "_id" : "New Hampshire", "cases" : 89983, "deaths" : 1261 }
{ "_id" : "Wisconsin", "cases" : 648094, "deaths" : 7402 }
{ "_id" : "Minnesota", "cases" : 547101, "deaths" : 7056 }
{ "_id" : "New Jersey", "cases" : 963484, "deaths" : 25006 }
{ "_id" : "Mississippi", "cases" : 308111, "deaths" : 7122 }
{ "_id" : "Maryland", "cases" : 430351, "deaths" : 8493 }
{ "_id" : "North Carolina", "cases" : 938784, "deaths" : 12325 }
{ "_id" : "Vermont", "cases" : 21488, "deaths" : 237 }
{ "_id" : "Rhode Island", "cases" : 143251, "deaths" : 2642 }
{ "_id" : "Guam", "cases" : 7851, "deaths" : 136 }
{ "_id" : "Ohio", "cases" : 1045945, "deaths" : 18917 }
{ "_id" : "Northern Mariana Islands", "cases" : 160, "deaths" : 2 }
{ "_id" : "Georgia", "cases" : 1078379, "deaths" : 19600 }
{ "_id" : "Virgin Islands", "cases" : 3005, "deaths" : 26 }
{ "_id" : "Maine", "cases" : 55374, "deaths" : 757 }
{ "_id" : "Illinois", "cases" : 1288844, "deaths" : 23865 }
{ "_id" : "Kentucky", "cases" : 434922, "deaths" : 6285 }
{ "_id" : "Indiana", "cases" : 701971, "deaths" : 13187 }
{ "_id" : "Louisiana", "cases" : 450673, "deaths" : 10264 }
{ "_id" : "North Dakota", "cases" : 105215, "deaths" : 1505 }
Type "it" for more
I can package all this is one JS script US_states.js
like this:
use covid19;
var cursor = db.us_only.aggregate([ { '$group': { '_id': '$state', 'cases': { '$sum': '$confirmed_daily' }, 'deaths': { '$sum': '$deaths_daily' } } } ]);
while (cursor.hasNext()) {
printjson(cursor.next());
}
And I can execute it like this directly from a shell:
$ mongo --quiet "mongodb+srv://readonly:readonly@covid-19.hip2i.mongodb.net/covid19" < US_states.js
switched to db covid19
{ "_id" : "Diamond Princess", "cases" : 49, "deaths" : 0 }
{ "_id" : "Idaho", "cases" : 184347, "deaths" : 2006 }
{ "_id" : "Nevada", "cases" : 309150, "deaths" : 5353 }
{ "_id" : "Texas", "cases" : 2840869, "deaths" : 49359 }
{ "_id" : "Wyoming", "cases" : 57127, "deaths" : 703 }
{ "_id" : "Nebraska", "cases" : 215074, "deaths" : 2226 }
{ "_id" : "South Dakota", "cases" : 120379, "deaths" : 1948 }
{ "_id" : "Missouri", "cases" : 590452, "deaths" : 8995 }
{ "_id" : "South Carolina", "cases" : 564931, "deaths" : 9304 }
{ "_id" : "District of Columbia", "cases" : 46209, "deaths" : 1088 }
{ "_id" : "Arkansas", "cases" : 332666, "deaths" : 5680 }
{ "_id" : "Virginia", "cases" : 640211, "deaths" : 10510 }
{ "_id" : "Utah", "cases" : 391177, "deaths" : 2161 }
{ "_id" : "Grand Princess", "cases" : 103, "deaths" : 3 }
{ "_id" : "Michigan", "cases" : 850583, "deaths" : 17694 }
{ "_id" : "Puerto Rico", "cases" : 117836, "deaths" : 2182 }
{ "_id" : "Louisiana", "cases" : 450673, "deaths" : 10264 }
{ "_id" : "Indiana", "cases" : 701971, "deaths" : 13187 }
{ "_id" : "Illinois", "cases" : 1288844, "deaths" : 23865 }
{ "_id" : "Kentucky", "cases" : 434922, "deaths" : 6285 }
{ "_id" : "Maine", "cases" : 55374, "deaths" : 757 }
{ "_id" : "North Dakota", "cases" : 105215, "deaths" : 1505 }
{ "_id" : "Georgia", "cases" : 1078379, "deaths" : 19600 }
{ "_id" : "Northern Mariana Islands", "cases" : 160, "deaths" : 2 }
{ "_id" : "Ohio", "cases" : 1045945, "deaths" : 18917 }
{ "_id" : "Virgin Islands", "cases" : 3005, "deaths" : 26 }
{ "_id" : "Guam", "cases" : 7851, "deaths" : 136 }
{ "_id" : "Rhode Island", "cases" : 143251, "deaths" : 2642 }
{ "_id" : "Vermont", "cases" : 21488, "deaths" : 237 }
{ "_id" : "North Carolina", "cases" : 938784, "deaths" : 12325 }
{ "_id" : "Maryland", "cases" : 430351, "deaths" : 8493 }
{ "_id" : "Mississippi", "cases" : 308111, "deaths" : 7122 }
{ "_id" : "New Jersey", "cases" : 963484, "deaths" : 25006 }
{ "_id" : "Minnesota", "cases" : 547101, "deaths" : 7056 }
{ "_id" : "Wisconsin", "cases" : 648094, "deaths" : 7402 }
{ "_id" : "New Hampshire", "cases" : 89983, "deaths" : 1261 }
{ "_id" : "Oklahoma", "cases" : 444228, "deaths" : 6697 }
{ "_id" : "Massachusetts", "cases" : 664943, "deaths" : 17427 }
{ "_id" : "New York", "cases" : 1973308, "deaths" : 51350 }
{ "_id" : "Washington", "cases" : 380338, "deaths" : 5357 }
{ "_id" : "Florida", "cases" : 2141686, "deaths" : 34164 }
{ "_id" : "Hawaii", "cases" : 32271, "deaths" : 473 }
{ "_id" : "Montana", "cases" : 106631, "deaths" : 1526 }
{ "_id" : "Colorado", "cases" : 483820, "deaths" : 6173 }
{ "_id" : "Pennsylvania", "cases" : 1090228, "deaths" : 25502 }
{ "_id" : "West Virginia", "cases" : 147203, "deaths" : 2756 }
{ "_id" : "Alabama", "cases" : 520780, "deaths" : 10728 }
{ "_id" : "California", "cases" : 3708716, "deaths" : 60717 }
{ "_id" : "Iowa", "cases" : 358395, "deaths" : 5857 }
{ "_id" : "Alaska", "cases" : 65536, "deaths" : 314 }
{ "_id" : "Arizona", "cases" : 851265, "deaths" : 17109 }
{ "_id" : "Oregon", "cases" : 172206, "deaths" : 2449 }
{ "_id" : "Delaware", "cases" : 99515, "deaths" : 1591 }
{ "_id" : "American Samoa", "cases" : 0, "deaths" : 0 }
{ "_id" : "Tennessee", "cases" : 827579, "deaths" : 12032 }
{ "_id" : "Connecticut", "cases" : 327298, "deaths" : 7984 }
{ "_id" : "Kansas", "cases" : 306886, "deaths" : 4900 }
{ "_id" : "New Mexico", "cases" : 194378, "deaths" : 3996 }
I also checked that the sum of all the cases and deaths for all the states above actually sum up to 31.421.359 cases and 564.402 deaths. Which looks accurate to me.
Does that help? Let me know if you need something else :-).
You can actually run any aggregation you want directly in the MongoDB Cluster and shape the result set you want.
Cheers,
Maxime.