Mongo数据如下,需求为统计指定class、function条件下,数据最多的年份月份,特留档
select y,m,count(*) as total from zhushou_stat where class="INDEX" and function="get_hot_days" and y*m >当前年*当前月 group by y,m order by total desc limit 1;
> db.zhushou_stat.find({function:"get_hot_days"})
{ "_id" : ObjectId("547857365b7181452f50fa4f"), "class" : "INDEX", "function" : "get_hot_days", "m" : 11, "y" : 2014, "add_time" : 1417172790 }
{ "_id" : ObjectId("5477e12b5b7181cc79d72c63"), "class" : "INDEX", "function" : "get_hot_days", "m" : 11, "y" : 2014, "add_time" : 1417142571 }
{ "_id" : ObjectId("5477e1345b71813d2a3740c3"), "class" : "INDEX", "function" : "get_hot_days", "m" : 10, "y" : 2014, "add_time" : 1417142580 }
{ "_id" : ObjectId("5477e13d5b71815b0fa180e9"), "class" : "INDEX", "function" : "get_hot_days", "m" : 10, "y" : 2014, "add_time" : 1417142589 }
{ "_id" : ObjectId("5477e13f5b7181500fc02fbd"), "class" : "INDEX", "function" : "get_hot_days", "m" : 10, "y" : 2014, "add_time" : 1417142591 }
{ "_id" : ObjectId("5477e1425b7181450f94d48f"), "class" : "INDEX", "function" : "get_hot_days", "m" : 12, "y" : 2014, "add_time" : 1417142594 }
{ "_id" : ObjectId("5477e4495b7181500fa0bf76"), "class" : "INDEX", "function" : "get_hot_days", "m" : 11, "y" : 2014, "add_time" : 1417143369 }
{ "_id" : ObjectId("5477e44b5b71813d2a1e3992"), "class" : "INDEX", "function" : "get_hot_days", "m" : 11, "y" : 2014, "add_time" : 1417143371 }
{ "_id" : ObjectId("5477e44d5b7181cc79688aee"), "class" : "INDEX", "function" : "get_hot_days", "m" : 11, "y" : 2014, "add_time" : 1417143373 }
{ "_id" : ObjectId("54784d615b71819d591c1101"), "class" : "INDEX", "function" : "get_hot_days", "m" : 5, "y" : 2015, "sid" : 1276701, "add_time" : 1417170273 }
{ "_id" : ObjectId("5478546d5b71819d59623356"), "class" : "INDEX", "function" : "get_hot_days", "m" : 4, "y" : 2015, "sid" : 1276701, "add_time" : 1417172077 }
{ "_id" : ObjectId("547857125b71819e591052d7"), "class" : "INDEX", "function" : "get_hot_days", "m" : 11, "y" : 2014, "add_time" : 1417172754 }
>
mongo查询语句:
db.zhushou_stat.aggregate(
[
{
$match:
{
class:"INDEX",
function:"get_hot_days"
},
},
{
$redact:
{
$cond:
{
if:
{
$gt:[ {$add:[{$multiply:["$y",100]},"$m"]}, 201400+12 ]
},
then:"$$KEEP",
else:"$$PRUNE"
}
}
},
{
$group:
{
_id:{ y:"$y", m:"$m" },
total:{ $sum:1 }
}
},
{
$sort:{ total:-1 }
},
{
$limit:1
}
],{explain:true, allowDiskUse:true}
)
php查询语句:
/**
* 统计:查看档期最多的月份
*
*/
public function getHotMonth()
{
$pipeline = array(
array(
'$match' => array(
'class' => 'INDEX',
'function' => 'get_hot_days'
)
),
array(
'$redact' => array(
'$cond' => array(
'if' => array(
'$gt' => array(
array(
'$add' => array(array('$multiply'=>array('$y', 100)), '$m')
),
date('Y')*100+date('m')
)
),
'then' => '$$KEEP',
'else' => '$$PRUNE'
)
)
),
array(
'$group' => array(
'_id' => array('y'=>'$y', 'm'=>'$m'),
'total' => array('$sum'=>1)
)
),
array(
'$sort' => array('total'=>-1)
),
array(
'$limit' => 1
)
);
$result = parent::getWriteMongo ( 'ZhushouStat' )->aggregate($<span style="font-family: Arial, Helvetica, sans-serif;">pipeline</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span>
文档上可以看到可以使用explain、allowDiskUse参数,但php作为aggregate第二个参数传入不能正确执行,还不清楚是不是因为扩展版本的问题