`

mongodb group by having where 查询sql

阅读更多
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250

db.orders.aggregate( [
   { $match: { status: 'A' } },
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   },
   { $match: { total: { $gt: 250 } } }
] )

 

coll.aggregate(
      [
        { $match: { msgType: 'text' } },
        { $group: { _id: '$content', count: { $sum: 1 } } },
        { $match: { count: { $gt: 1 } } },
        { $sort: { count: -1 } }
      ]
    ).toArray(function(err, result) {
      console.log(result);
    });

[ { _id: '猜谜底', count: 7 },
  { _id: 'test', count: 5 },
  { _id: 'help1', count: 4 },
  { _id: '百科help1', count: 3 } ]   

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics