MongoDB aggregate with PHP - group by on date - php

I am using an aggregate in MongoDB with PHP. The code looks like:
$results = $c->aggregate(array(
array(
'$project' => array(
'day' => array('$dayOfYear' => '$executed')
),
),
array(
'$group' => array(
'_id' => array('day' => '$day'),
'count' => array('$sum' => 1)
),
),
array(
'$sort' => array(
'_id' => 1
),
),
array(
'$limit' => 30
)
));
The problem with this, is that $dayOfYear does not sort correctly, because it sorts 2 then 3 then 345, 346... I need it to be date ascending. So, basically instead of simply doing $dayOfYear I need something like $year-$month-$dayOfMonth.
Unfortunately this does not work. Any ideas?
Thanks.

You can project those parts out and then group on them to enable you to group on the whole date:
$results = $c->aggregate(array(
array(
'$project' => array(
'year' => array('$year' => '$executed' ),
'month' => array('$month' => '$executed' ),
'day' => array('$dayOfMonth' => '$executed')
),
),
array(
'$group' => array(
'_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
'count' => array('$sum' => 1)
),
),
array(
'$sort' => array(
'_id.year' => 1,
'_id.month' => 1,
'_id.day' => 1
),
),
array(
'$limit' => 30
)
));
Something like that should do the trick allowing you to sort on, as you stated: $year-$month-$dayOfMonth.

You shouldn't sort on the entire _id because you've set that up to contain an object (which can sort oddly), so change your $sort to this instead to specifically sort by day of year:
'$sort' => array(
'_id.day' => 1
),

Related

How do I group by on a dbref in MongoDB with Doctrine

I want to do an aggregation query where I can group by a dbref. This is what I've tried.
$aggregation = array(
array(
'$group' => array(
'_id' => '$foreign.$id',
'doc' => array(
'$last' => '$$ROOT'
)
)
),
array(
'$sort' => array(
'date' => -1
)
)
);
return $this->getDocumentManager()->getDocumentCollection('AppBundle:Collection')->aggregate($aggregation)->toArray();
But this attempt fails because you're not allowed to use the second $ sign in '$foreign.$id'. How can I do this query?
After a lot of searching I've found this Bug Ticket on the monogdb Jira which deals with basically this / an similar issue.
The last comment on this ticket offers an workaround to resolve the issue as an command line code. I've taken this solution and built it into an aggregation query which solved my problem.
$aggregation = array(
array(
'$addFields' => array(
'foreignId' => array(
'$arrayToObject' => array(
'$map' => array(
'input' => array(
'$objectToArray' => '$foreign'
),
'in' => array(
'k' => array(
'$cond' => array(
array(
'$eq' => array(
array(
'$substrCP' => array(
'$$this.k', 0, 1
)
),
array(
'$literal' => '$'
)
)
),
array(
'$substrCP' => array(
'$$this.k',1,['$strLenCP' => '$$this.k']
)
),
'$$this.k'
)
),
'v' => '$$this.v'
)
)
)
)
)
),
array(
'$group' => array(
'_id' => '$foreignId',
'doc' => array(
'$last' => '$$ROOT'
)
)
),
array(
'$sort' => array(
'date' => -1
)
)
);
$this->getDocumentManager()->getDocumentCollection('AppBundle:Collection')->aggregate($aggregation)->toArray();
This query gives me the correct and expected result.

How to merge multiple dimension array cakephp

I just using function query() of cakePhp. The query will return array something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
)
)
)
Now, I want to check if array have the same cate.date will merge array (in this case is elements 0,1 of my array). Output something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '11',
'price' => '150.000'
),
(int) 1 => array(
'rel_data_category' => '10',
'price' => '250.000'
)
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '10'
'price' => '250.000'
)
)
)
)
Please help!
You will need to loop through the results and build a new array with the data in the form you want. You can use the CakePHP Hash::extract() method to map the dates to indexes so that you can combine the data for the dates.
For example:-
// Get out all the dates available and then flip the array so that we have a map of dates to indexes
$dates = Hash::extract($results, '{n}.cate.date');
$datesMap = array_flip($dates);
// Define an empty array that we will store the new merged data in
$data = [];
// Loop through the query results and write to the $data array using the map of dates
foreach ($results as $result) {
$key = $datesMap[$result['cate']['date']];
$data[$key]['cate'] = $result['cate'];
$data[$key]['cate_detail'][] = [
'rel_data_category' => $result['cate_detail']['rel_data_category'],
'price' => $result['cate_item']['price']
];
}

Unique Merge multiple multidimensional array

I have some difficulties to merge many multidimensional array in php. I tried to do it by many way, but each time, I don't get the result wanted. I tried with array_merge(array_unique,...) and in different post I found a way with array_map, but I don't understand everything...
I can have many multi array like below:
array(
(int) 0 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'AM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
),
(int) 1 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'PM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
)
And I would like to get this kind of array :
array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'Full day'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
Do you have some advices to give me to get this result ?
Thank you very much !
I don't know if the best solution but it seems to work like this, and fastly :
foreach ($users as $k=>$v){
//$r[$k] = array_merge($v,$users[$k]);
//$unique[] = array_map("unserialize", array_unique(array_map("serialize", $users[$k])));
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => $v['Calendar']['period']
);
if ($k > 0) {
if (in_array($v['User']['username'],$s[$k])) {
unset($s[$k-1]);
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => "FD"
);
}
}
}
Do you have another idea or this one is enough good ?
thank you !

How can i get information by manipulating two arrays in php?

I have two array one for present list and other for total lectures. i want a third array that will hold the percent of two
My first array (present days list)
$allpresentAttInfo = array(
0 => array(
'year' => '2013',
'term' => 'T1',
'presentDays' => '123'
),
1 => array(
'year' => '2013',
'term' => 'T2',
'presentDays' => '112'
)
);
My Second array (Total days list)
$allAttInfo = array(
0 => array(
'year' => '2013',
'term' => 'T1',
'totalDays' => '200'
),
1 => array(
'year' => '2013',
'term' => 'T2',
'totalDays' => '216'
)
);
My Resultant array should be like this
$attInfo = array(
0 => array(
'year' => '2013',
'term' => 'T1',
'presentPercent' => '63.7 %'
),
1 => array(
'year' => '2013',
'term' => 'T2',
'presentPercent' => '42.7 %'
)
);
So by merging both the arrays i will have to find the present present in given year and term. How to achieve this on PHP side. Thanks in advance
Well thanks guys .. I was in a hurry so searching for some short-cut copy paste way ... Well i coded it. but still give me some performance optimization if available.
My Solution is.
$attInfo = array();
foreach ($allAttInfo as $allatt) {
foreach ($allpresentAttInfo as $allpre) {
if($allpre['year'] == $allatt['year'] && $allpre['term'] == $allatt['term']){
$presentPercent = round(($allpre['presentDays'] / $allatt['totalDay'])*100,2) . "%";
$newArray=array('year'=>$allpre['year'],'term'=>$allpre['term'],'presentPercent'=>$presentPercent);
array_push($attInfo, $newArray);
}
}
}

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)

Categories