mongo的写法与mysql等sql有着天壤之别,如最近在统计爬虫抓取的数据,其中一个就是按字段从大到小取前十个:
sql写法:select count(id) from invest group by city desc limit 0,10
mongo的写法则为:var group=[{$group:{_id:"$city", count:{$sum:1}}}, {$sort:{count: -1}}, {$limit:10}]; db.invest.aggregate(group);
结果如下: