SQL
Mongo
SELECT MAX(price) AS 'max', AVG(price) AS 'avg', MIN(price) AS 'min' FROM dbo.orders WHERE ord_date>='2013/10/1' AND ord_date<'2013/11/1'
db.orders.aggregate([ {$match: { ord_date:{ $gte:new Date('2013/10/1'), $lt:new Date('2013/11/1')}}}, {$group: { _id:null, max:{$max:'$price'}, avg:{$avg:'$price'}, min:{$min:'$price'}}} ])
SELECT TOP 20 cust_id, status, count(1) AS 'count' FROM dbo.orders WHERE ord_date>='2013/10/1' AND ord_date<'2013/11/1' GROUP BY cust_id, status ORDER BY 'count' DESC
db.orders.aggregate([ {$match:{act:act, ord_date:{$gte:new Date('2013/10/1'), $lt:new Date('2013/11/1')}}}, {$group:{ _id:{cust_id:'$cust_id', status:'$status'}, count:{$sum:1}}}, {$sort:{count:-1}}, {$limit:20} ])
SELECT status AS '_id', sum(price) AS 'total' FROM dbo.orders WHERE ord_date>='2013/10/1' AND ord_date<'2013/11/1' GROUP BY status
db.orders.aggregate([ {$match: { ord_date:{$gte:new Date('2013/10/1'), $lt:new Date('2013/11/1')}}}, {$group: {_id: '$status', total: {$sum: '$price'}}} ])
其他範例在MongoDB的官方文件範例上都差不多有,我就不重複了,至於要用Join的方式我還沒試過,而更複雜的查詢就要用MapReduce了。
No comments:
Post a Comment