mongodb group by操作

原文链接:https://www.mkyong.com/mongodb/mongodb-group-count-and-sort-example/

1. Test Data

whois_range collection, containing many records.

> db.whois_range.find();
{
	"_id" : 1,
	"country" : "us",
	"source" : "ARIN",
	"status" : "NEW",
	"createdDate" : ISODate("2016-05-03T08:52:32.434Z")
},
{
	"_id" : 2,
	"country" : "us",
	"source" : "ARIN",
	"status" : "NEW",
	"createdDate" : ISODate("2016-05-03T09:52:32.434Z")
},
{
	"_id" : 3,
	"country" : "cn",
	"source" : "APNIC",
	"status" : "NEW",
	"createdDate" : ISODate("2016-05-03T10:52:32.434Z")
},
{
	"_id" : 4,
	"country" : "eu",
	"source" : "RIPE",
	"status" : "NEW",
	"createdDate" : ISODate("2016-05-03T10:52:32.434Z")
},
{...}

 

P.S “Source” = RIPE, AFRINIC, KRNIC, LACNIC, APNIC, JPNIC and ARIN

2. Group and Count example

Group by “source”, and count the total number of “source”.

> db.whois_range.aggregate([
		{"$group" : {_id:"$source", count:{$sum:1}}}
	])
	
{ "_id" : "RIPE", "count" : 29270 }
{ "_id" : "AFRINIC", "count" : 1326 }
{ "_id" : "KRNIC", "count" : 105 }
{ "_id" : "LACNIC", "count" : 5889 }
{ "_id" : "APNIC", "count" : 6644 }
{ "_id" : "JPNIC", "count" : 167 }
{ "_id" : "ARIN", "count" : 25429 }

 

3. Group by multiple ids example

Group by two ids: “source” and “status”.

> db.whois_range.aggregate([
		{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}} ])
	])
	
{ "_id" : { "source" : "RIPE", "status" : "NEW" }, "count" : 29260 }
{ "_id" : { "source" : "RIPE", "status" : "ERROR" }, "count" : 10 }
{ "_id" : { "source" : "LACNIC", "status" : "NEW" }, "count" : 5889 }
{ "_id" : { "source" : "KRNIC", "status" : "NEW" }, "count" : 105 }
{ "_id" : { "source" : "APNIC", "status" : "NEW" }, "count" : 6644 }
{ "_id" : { "source" : "AFRINIC", "status" : "NEW" }, "count" : 1326 }
{ "_id" : { "source" : "JPNIC", "status" : "NEW" }, "count" : 167 }
{ "_id" : { "source" : "ARIN", "status" : "NEW" }, "count" : 25420 }
{ "_id" : { "source" : "ARIN", "status" : "DONE" }, "count" : 9 }

 

3. Group, Count, and Sort example

3.1 Group by two ids: “source” and “status”, count the total number of records, and sort by “source”.

> db.whois_range.aggregate([
	{"$group" : 
		{_id:{source:"$source",status:"$status"}, count:{$sum:1}}
	}, 
	{$sort:{"_id.source":1}}
])

{ "_id" : { "source" : "AFRINIC", "status" : "NEW" }, "count" : 1326 }
{ "_id" : { "source" : "APNIC", "status" : "NEW" }, "count" : 6644 }
{ "_id" : { "source" : "ARIN", "status" : "NEW" }, "count" : 25420 }
{ "_id" : { "source" : "ARIN", "status" : "DONE" }, "count" : 9 }
{ "_id" : { "source" : "JPNIC", "status" : "NEW" }, "count" : 167 }
{ "_id" : { "source" : "KRNIC", "status" : "NEW" }, "count" : 105 }
{ "_id" : { "source" : "LACNIC", "status" : "NEW" }, "count" : 5889 }
{ "_id" : { "source" : "RIPE", "status" : "NEW" }, "count" : 29260 }
{ "_id" : { "source" : "RIPE", "status" : "ERROR" }, "count" : 10 }

 

3.2 Sort by “count”, descending order.

> db.whois_range.aggregate([
	{"$group" : 
		{_id:{source:"$source",status:"$status"}, count:{$sum:1}}
	}, 
	{$sort:{"count":-1}}
])

{ "_id" : { "source" : "RIPE", "status" : "NEW" }, "count" : 29260 }
{ "_id" : { "source" : "ARIN", "status" : "NEW" }, "count" : 25420 }
{ "_id" : { "source" : "APNIC", "status" : "NEW" }, "count" : 6644 }
{ "_id" : { "source" : "LACNIC", "status" : "NEW" }, "count" : 5889 }
{ "_id" : { "source" : "AFRINIC", "status" : "NEW" }, "count" : 1326 }
{ "_id" : { "source" : "JPNIC", "status" : "NEW" }, "count" : 167 }
{ "_id" : { "source" : "KRNIC", "status" : "NEW" }, "count" : 105 }
{ "_id" : { "source" : "RIPE", "status" : "ERROR" }, "count" : 10 }
{ "_id" : { "source" : "ARIN", "status" : "DONE" }, "count" : 9 }

 

References

  1. MongoDB Aggregation manual
  2. MongoDB sort manual
  3. MongoDB – Aggregate and Group example
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值