在本教程中,我们将向您展示如何使用MongoDB聚合函数对文档(数据)进行分组。
1.测试数据
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" }
导入到“网站”集合中。
> 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以下示例按“主机”字段分组,并显示每个主机的总和。
> 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
添加$sort
。
> db.website.aggregate(
{
$group : {_id : "$hosting", total : { $sum : 1 }}
},
{
$sort : {total : -1}
}
);
输出–以降序显示“总计”。 对于升序,请使用$sort : {total : 1}
。
{
"result" : [
{
"_id" : "aws.amazon.com",
"total" : 4
},
{
"_id" : "hostgator.com",
"total" : 3
},
{
"_id" : "cloud.google.com",
"total" : 2
},
{
"_id" : "godaddy.com",
"total" : 1
}
],
"ok" : 1
}
2.3添加$match
条件,仅通过“托管”为“ aws.amazon.com”分组。
> db.website.aggregate(
{
$match : {hosting : "aws.amazon.com"}
},
{
$group : { _id : "$hosting", total : { $sum : 1 } }
}
);
输出量
{
"result" : [
{
"_id" : "aws.amazon.com",
"total" : 4
}
],
"ok" : 1
}
更多例子
有关更多高级聚合和组示例,请参阅此官方的MongoDB聚合指南 。
3.将分组结果导出为CSV或JSON
通常,我们需要以csv或JSON格式导出分组结果。 要解决此问题,请将组结果插入新集合中,然后通过mongoexport
导出新集合。
3.1将组结果设置为变量。 在这种情况下,变量名称为“ groupdata”。
> var groupdata = db.website.aggregate(
{
$group : {_id : "$hosting", total : { $sum : 1 }}
},
{
$sort : {total : -1}
}
);
3.2将 groupdata.toArray()
插入新集合。
> db.websitegroup.insert(groupdata.toArray());
> db.websitegroup.find().pretty()
{ "_id" : "aws.amazon.com", "total" : 4 }
{ "_id" : "hostgator.com", "total" : 3 }
{ "_id" : "cloud.google.com", "total" : 2 }
{ "_id" : "godaddy.com", "total" : 1 }
>
3.3将集合“ websitegroup”导出到一个csv文件。
c:\> mongoexport -d testdb -c websitegroup -f _id,total -o group.csv --csv
connected to: 127.0.0.1
exported 4 records
_id,total
"aws.amazon.com",4.0
"cloud.google.com",2.0
"godaddy.com",1.0
"hostgator.com",3.0
3.4将集合“ websitegroup”导出到JSON文件。
c:\> mongoexport -d testdb -c websitegroup -o group.json
connected to: 127.0.0.1
exported 4 records
{ "_id" : "aws.amazon.com", "total" : 4 }
{ "_id" : "cloud.google.com", "total" : 2 }
{ "_id" : "godaddy.com", "total" : 1 }
{ "_id" : "hostgator.com", "total" : 3 }
4.大型分类操作
在2.6版中进行了更改 –阅读此内存限制
在MongoDB中,内存中的排序限制为100M,要执行大型排序,需要启用allowDiskUse
选项将数据写入临时文件以进行排序。
为避免超出内存排序限制错误,请启用allowDiskUse
选项。
db.website.aggregate(
[
{$group : {_id : "$hosting", total : { $sum : 1 }}},
{$sort : {total : -1}}
],
{allowDiskUse: true}
);
参考文献
翻译自: https://mkyong.com/mongodb/mongodb-aggregate-and-group-example/