Tyzak
Tyzak

Reputation: 2436

Define MongoDB Querys

I would like to define two querys but despite the mongoDB examples I don't really know /understood how to do that:

Query1:

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

Query2:

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

Regards

Upvotes: 0

Views: 65

Answers (1)

Asya Kamsky
Asya Kamsky

Reputation: 42362

For the first one, aggregation query would be:

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

For the second one you do the same but you add a $match phase instead of the where clause and you group by multiple fields:

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"} 
    }}
]);

Upvotes: 1

Related Questions