mongo group by

mac2022-06-30  65

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);

结果如下:

转载于:https://www.cnblogs.com/chimmu/p/6212306.html

最新回复(0)