0

我想定义两个查询,但尽管有 mongoDB 示例,我真的不知道/理解如何做到这一点:

查询1:

SELECT COUNT(*), SUM(PKTS), SUM(BYTES) FROM mytable

查询2:

SELECT srcad, srcpo, dstad, dstpo, pro, COUNT(*), SUM(PKTS), SUM(BYTES) FROM mytable WHERE scrpo=80 OR dstpo=80 GROUP BY srcad, scrpo, dstad, dstpo, pro

问候

4

1 回答 1

1

对于第一个,聚合查询将是:

db.mytable.aggregate({$group:{_id:1, sump:{$sum:"$pkts"}, sumb:{$sum:"$bytes"} }});

对于第二个,您执行相同操作,但添加了 $match 阶段而不是 where 子句,并按多个字段分组:

db.mytable.aggregate([ 
   {$match:{$or:[{scrpo:80},{dstpo:80}]} },
   {$group:{_id :  {srcad:"$srcad",scrpo:"$scrpo",
                    dstad:"$dstad",dstpo:"$dstpo",pro:"$pro"},
            sump:{$sum:"$pkts"}, 
            sumb:{$sum:"$bytes"} 
    }}
]);
于 2013-03-20T04:09:22.950 回答