mongo aggregate聚合查询

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第二个参数传入不能正确执行,还不清楚是不是因为扩展版本的问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值