bhaden94 / Covid19-tracker-V2-API

Spring Boot API and backend for version 2 of Covid-19 tracker.
https://covid-tracker-v2.herokuapp.com/
MIT License
1 stars 0 forks source link

Line Chart Routes #16

Closed bhaden94 closed 3 years ago

bhaden94 commented 3 years ago

2 ways to do this.

  1. Get all docs form DB and write custom aggregation Java code.
  2. Create a custom MongoDB aggregation query and run that
bhaden94 commented 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
    },
bhaden94 commented 3 years ago

How to implement this in Spring Boot

bhaden94 commented 3 years ago

Aggregating for all states is now working.

bhaden94 commented 3 years ago

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
    }
  }
]