?
在本文章中,会演示MongoDB使用聚集函数对文档进行分组的用法。
1.测试数据
网站主机域名列表的JSON格式文件
?
?
website.json { "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" } { "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com"} { "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" } { "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" } { "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" } { "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" } { "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" } { "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" } { "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" } { "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }
?
?
导入website文档
?
?
> mongoimport -d testdb -c website --file website.json connected to: 127.0.0.1 Mon Jan 13 14:30:22.662 imported 10 objects
?
?
?
注意 如果是已存在的文档, 加 --upsert 选项来覆盖数据. > mongoimport -d testdb -c website --file website.json --upsert
?
?
2.分组例子
使用db.collection.aggregate和$group函数进行文档分组
? 2.1 下面的例子通过“hosting”字段分组并显示每个主机总数
??
> db.website.aggregate( { $group : {_id : "$hosting", total : { $sum : 1 }} } );
? 结果
?
??
??
{ "result" : [ { "_id" : "godaddy.com", "total" : 1 }, { "_id" : "cloud.google.com", "total" : 2 }, { "_id" : "aws.amazon.com", "total" : 4 }, { "_id" : "hostgator.com", "total" : 3 } ], "ok" : 1 }
?
?
用SQL可以表示为:
??
SELECT hosting, SUM(hosting) AS total FROM website GROUP BY hosting
?
?2.2 使用$sort函数进行排序