简体   繁体   中英

Grouping documents by Range in mongodb

I am working on MongoDB to get the record by query filtering with the endpoints like http://localhost:8000/api/data?c=AT&a=2021-W01&b=2021-W20 I got the result perfectly like

My MongoDB Query:

{
    $match:{ ReportingCountry:c,
    YearWeekISO:{$gte:a,$lt:b}}
}

Result

[
    {
        "_id": "62f8e16fd2c20d2eb6165bf0",
        "YearWeekISO": "2021-W01",
        "ReportingCountry": "AT",
        "Denominator": "7388778",
        "NumberDosesReceived": "0",
        "NumberDosesExported": "0",
        "FirstDose": "0",
        "SecondDose": "0",
        "DoseAdditional1": "0",
        "DoseAdditional2": "0",
        "UnknownDose": "0",
        "Region": "AT",
        "TargetGroup": "ALL",
        "Vaccine": "MOD",
        "Population": "8932664",
        "__v": 0
    },
    {
        "_id": "62f8e16fd2c20d2eb6165c0a",
        "YearWeekISO": "2021-W01",
        "ReportingCountry": "AT",
        "Denominator": "3002329",
        "NumberDosesReceived": "0",
        "NumberDosesExported": "0",
        "FirstDose": "0",
        "SecondDose": "0",
        "DoseAdditional1": "0",
        "DoseAdditional2": "0",
        "UnknownDose": "0",
        "Region": "AT",
        "TargetGroup": "Age25_49",
        "Vaccine": "JANSS",
        "Population": "8932664",
        "__v": 0
    },
    {
        "_id": "62f8e16fd2c20d2eb6165c23",
        "YearWeekISO": "2021-W01",
        "ReportingCountry": "AT",
        "Denominator": "753524",
        "NumberDosesReceived": "0",
        "NumberDosesExported": "0",
        "FirstDose": "0",
        "SecondDose": "0",
        "DoseAdditional1": "0",
        "DoseAdditional2": "0",
        "UnknownDose": "0",
        "Region": "AT",
        "TargetGroup": "Age70_79",
        "Vaccine": "JANSS",
        "Population": "8932664",
        "__v": 0
    }

But the next step is to group date by range if I enter five the query like http://localhost:8000/api/data?c=AT&a=2021-W19&b=2021-W20&range=5 the result should come up like

{
"summary": [
{
"weekStart": "2020-W10",
"weekEnd": "2020-W15",
"NumberDosesReceived": 1000
},
{
"weekStart": "2020-W15",
"weekEnd": "2020-W20"
"NumberDosesReceived": 2000 } ,
… till end of range (dateTo)
]

How can I acheive this with Mongodb

It should work

const YearWeekISO = { $toDate: "$YearWeekISO" };

 { $project: { fiveWeekperiod: { $subtract: [ { $week: YearWeekISO }, { $mod: [{ $week: YearWeekISO }, 5] }, ], }, date: YearWeekISO, NumberDosesReceived: 1, }, }, { $group: { _id: { year: { $year: "$date" }, fiveWeek: "$fiveWeekperiod", }, weekStart: { $min: "$date" }, weekEnd: { $max: "$date" }, NumberDosesReceived: { $sum: "$NumberDosesReceived" }, }, }, { $project: { _id: 0, weekStart: { $dateToString: { date: "$weekStart", format: "%GW%V", }, }, weekEnd: { $dateToString: { date: { $dateAdd: { startDate: "$weekEnd", unit: "week", amount: 1, }, }, format: "%GW%V", }, }, NumberDosesReceived: 1, }, }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM