Closed bhaden94 closed 3 years ago
This aggregation query works for what we need. It needs to be put into Spring Boot on a route.
db.us_collection.aggregate(
[
{
"$project": {
"_id": "$_id",
"date": "$date",
"confirmed" : { "$sum" : "$states.confirmed"},
"deaths" : { "$sum" : "$states.deaths"},
"recovered" : { "$sum" : "$states.recovered"},
"active" : { "$sum" : "$states.active"},
}
},
{
$sort: { "date": 1}
},
]
)
It will return this:
{
"date": "2020-04-12",
"confirmed": 555613,
"deaths": 22107,
"active": 500306,
"recovered": 67139
},
{
"date": "2020-04-13",
"confirmed": 580876,
"deaths": 23633,
"active": 615609,
"recovered": 78924
},
{
"date": "2020-04-14",
"confirmed": 607926,
"deaths": 25931,
"active": 534076,
"recovered": 85408
},
Aggregating for all states is now working.
Implementing with a specific state:
[
{
'$unwind': {
'path': '$states'
}
}, {
'$match': {
'states.state': 'california'
}
}, {
'$project': {
'_id': '$_id',
'date': '$date',
'confirmed': {
'$sum': '$states.confirmed'
},
'deaths': {
'$sum': '$states.deaths'
},
'recovered': {
'$sum': '$states.recovered'
},
'active': {
'$sum': '$states.active'
}
}
}, {
'$sort': {
'date': 1
}
}
]
2 ways to do this.