[COLOR=#000000][FONT=monospace]I have a table called “Work”. Some sample data is below. I am trying to sum up the amount of minutes a user (or team) worked in a date period grouped by task. However I am getting sums as zero when its not correct.
Data
[/FONT][/COLOR][
- {
[LIST] - _id:
{
[LIST] - $id: “52a5ad1fb3f7e2b8011b0000”
[/LIST]
}, - date:
{
[LIST] - sec: 1387929600,
- usec: 0
[/LIST]
}, - user:
{
[LIST] - $id: “52a1e6a3b3f7e20e1e120000”
[/LIST]
}, - client:
{
[LIST] - $id: “52a1e6a3b3f7e20e1e050000”
[/LIST]
}, - task: “design”,
- minutes: “1:00”,
- minutes_numeric: 60,
- deleted: false,
- organisation_id: 1,
- updated:
{
[LIST] - sec: 1386589471,
- usec: 435000
[/LIST]
}, - created:
{
[LIST] - sec: 1386589471,
- usec: 435000
[/LIST]
}, - id: “52a5ad1fb3f7e2b8011b0000”,
- team: false
[/LIST]
}, - {
[LIST] - _id:
{
[LIST] - $id: “52a5ad1fb3f7e2b8011c0000”
[/LIST]
}, - date:
{
[LIST] - sec: 1387929600,
- usec: 0
[/LIST]
}, - user:
{
[LIST] - $id: “52a1e6a3b3f7e20e1e120000”
[/LIST]
}, - client:
{
[LIST] - $id: “52a1e6a3b3f7e20e1e070000”
[/LIST]
}, - task: “planning”,
- minutes: “0:15”,
- minutes_numeric: 15,
- deleted: false,
- organisation_id: 1,
- updated:
{
[LIST] - sec: 1386589471,
- usec: 435000
[/LIST]
}, - created:
{
[LIST] - sec: 1386589471,
- usec: 435000
[/LIST]
}, - id: “52a5ad1fb3f7e2b8011c0000”,
- team: false
[/LIST]
}, - {
[LIST] - _id:
{
[LIST] - $id: “52a5ad1fb3f7e2b8011d0000”
[/LIST]
}, - date:
{
[LIST] - sec: 1387929600,
- usec: 0
[/LIST]
}, - user:
{
[LIST] - $id: “52a1e6a3b3f7e20e1e120000”
[/LIST]
}, - client:
{
[LIST] - $id: “52a1e6a3b3f7e20e1e080000”
[/LIST]
}, - task: “design”,
- minutes: “1:00”,
- minutes_numeric: 60,
- deleted: false,
- organisation_id: 1,
- updated:
{
[LIST] - sec: 1386589471,
- usec: 435000
[/LIST]
}, - created:
{
[LIST] - sec: 1386589471,
- usec: 435000
[/LIST]
}, - id: “52a5ad1fb3f7e2b8011d0000”,
- team: false
[/LIST]
},
[COLOR=#000000][FONT=monospace]
[/FONT][/COLOR]Result
result:
[
- {
[LIST] - _id: “creative”,
- value: 0
[/LIST]
},
- {
[LIST] - _id: " planning",
- value: 0
[/LIST]
},
- {
[LIST] - _id: “design”,
- value: 0
[/LIST]
},
PHP
$collection = $this->db->Work;
$out = $collection->aggregate(
array(
'$match' => array(
'user' => new MongoId($user),
'date' => array('$gte' => $start, '$lte' => $end)
),
),
array('$group' => array(
'_id' => '$task',
'value' => array('$sum' => 'minutes_numeric')
)
)
);