sql >> Base de Datos >  >> NoSQL >> MongoDB

Cómo agregar datos por cada día, semana, mes sabio en la semana, mes, año correspondiente respectivamente en mongodb

Para todas las fechas y horas relacionadas con la consulta, debe $ proyectar los valores del campo de fecha - manual aquí

El marco de agregación viene con ayuda en este caso:consulte la consulta básica a continuación, que tiene un agregado de peso diario y semanal, para que pueda transformar esta consulta para otros períodos de tiempo o usar una consulta por período:

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

Los resultados de ejemplo para el mes 3 para mis datos ficticios se encuentran a continuación:

{
    "_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
        }
    ]
}

y para formar la forma según sea necesario, puede usar $ fase del proyecto

   {$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
}

¡Cualquier comentario es bienvenido!