sql >> Database teknologi >  >> NoSQL >> MongoDB

Sådan aggregeres data efter hver dag, uge, måned i den tilsvarende uge, måned, år i mongodb

For al dato og klokkeslæt forbundet med forespørgslen skal du $projektere dets værdier fra datofeltet - manual her

Aggregeringsramme kommer med hjælp i dette tilfælde - se venligst den grundlæggende forespørgsel nedenfor, som har daglig og ugentlig vægtaggregering, så du kan transformere denne forespørgsel til andre tidsperioder eller bruge én forespørgsel pr. periode:

db.timing.aggregate([{
        $project : {
            year : {
                $year : "$date"
            },
            month : {
                $month : "$date"
            },
            week : {
                $week : "$date"
            },
            day : {
                $dayOfWeek : "$date"
            },
            _id : 1,
            weight : 1
        }
    }, {
        $group : {
            _id : {
                year : "$year",
                month : "$month",
                week : "$week",
                day : "$day"
            },
            totalWeightDaily : {
                $sum : "$weight"
            }
        }
    },
    {
        $group : {

            _id : {
                year : "$_id.year",
                month : "$_id.month",
                week : "$_id.week"
            },
            totalWeightWeekly : {
                $sum : "$totalWeightDaily"
            },
            totalWeightDay : {
                $push : {
                    totalWeightDay : "$totalWeightDaily",
                    dayOfWeek : "$_id.day"
                }
            }
        }
    }, {
        $match : {
            "_id.month" : 3
        }
    }
])

Eksempelresultater for måned 3 for mine dummy-data er nedenfor:

{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 10
    },
    "totalWeightWeekly" : 600,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 7
        }, 
        {
            "totalWeightDay" : 400,
            "dayOfWeek" : 6
        }
    ]
}
{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 9
    },
    "totalWeightWeekly" : 1000,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 4
        }, 
        {
            "totalWeightDay" : 600,
            "dayOfWeek" : 3
        }, 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 7
        }
    ]
}
{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 12
    },
    "totalWeightWeekly" : 400,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 7
        }, 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 2
        }
    ]
}
{
    "_id" : {
        "year" : 2016,
        "month" : 3,
        "week" : 13
    },
    "totalWeightWeekly" : 200,
    "totalWeightDay" : [ 
        {
            "totalWeightDay" : 200,
            "dayOfWeek" : 3
        }
    ]
}

og for at danne form efter behov kan du bruge $project phase

   {$project:{
                _id:0,
                "year" : "$_id.year", //this could be ommited but use $match to avoid sum of other years
                "month" : "$_id.month",  //this could be ommited but use $match to avoid sum of other months
                "week" :"$_id.week",                
                totalWeightWeekly:1

                }}
{
    "totalWeightWeekly" : 600,
    "year" : 2016,
    "month" : 3,
    "week" : 10
}

Alle kommentarer er velkomne!




  1. Sådan sammenkædes strenge i SQL

  2. MongoDB får max dato inde i dobbelt indlejret array

  3. Hvordan indsætter man den samme dato i mongodb som læst fra String gennem java?

  4. Node indsæt store data ved hjælp af mongoose