简体   繁体   中英

mongoDB PHP aggregate collection by date and sum field per date

I am attempting to output an array of dates grouped by date with a sum of a field called minutes_numeric .

My current query is:

return $this->db->Work->aggregate(array(
            array(
                '$match' => array(
                    'date' => array('$gt' => $start, '$lt' => $end)
                )
            ),
            array(
                '$project' => array(
                    'year' => array('$year' => '$date' ),
                    'month' => array('$month' => '$date' ),
                    'day' => array('$dayOfMonth' => '$date'),
                ),
            ),
            array(
                '$group' => array(
                    '_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
                    'minutes_numeric' => array('$sum' => 1)
                ),
            ),
            array(
                '$sort' => array(
                    '_id' => 1
                ),
            ),
            array(
                '$limit' => 30
            )
        ));

Which outputs:

{
result: [
{
_id: {
year: 2013,
month: 12,
day: 4
},
minutes_numeric: 40
},
{
_id: {
year: 2013,
month: 12,
day: 11
},
minutes_numeric: 127
},
{
_id: {
year: 2013,
month: 12,
day: 12
},
minutes_numeric: 108
}
],
ok: 1
}

Which is great, however the minutes_numeric represents the number of rows for that day, so I changed the $group clause:

    array(
        '$group' => array(
            '_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
            'minutes_numeric' => array('$sum' => '$minutes_numeric')
        ),
    ),

Which then changes the sum (minutes_numeric) to 0 and not an actual sum.

I'm unsure why my sum is going wrong, any help is greatly appreciated


Edit,

If I update the $project to include minutes_numeric then the sum seems to exclude the $match parameters

 array(
            '$project' => array(
                'year' => array('$year' => '$date' ),
                'month' => array('$month' => '$date' ),
                'day' => array('$dayOfMonth' => '$date'),
                'minutes_numeric' => 1
            ),
        ),
        array(
            '$group' => array(
                '_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
                'minutes_numeric' => array('$sum' => '$minutes_numeric')
            ),
        )

If this is you current query

return $this->db->Work->aggregate(array(
            array(
                '$match' => array(
                    'date' => array('$gt' => $start, '$lt' => $end)
                )
            ),
            array(
                '$project' => array(
                    'year' => array('$year' => '$date' ),
                    'month' => array('$month' => '$date' ),
                    'day' => array('$dayOfMonth' => '$date'),
                    'minutes_numeric' => 1
                ),
            ),
            array(
                '$group' => array(
                    '_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
                    'minutes_numeric' => array('$sum' => '$minutes_numeric')
                ),
            ),
            array(
                '$sort' => array(
                    '_id' => 1
                ),
            ),
            array(
                '$limit' => 30
            )
        ));

then the only way your $match parameters are "ignored" and the results are not as you expect is probably because they are wrong. Try checking the values of $start and $end, because the query you are using seems correct.

In case that is not the problem please update your question with the results of your current query. (Also update your query if it is different than the one in my answer)

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