在本文章中,会演示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函数进行排序
> db.website.aggregate( { $group : {_id : "$hosting", total : { $sum : 1 }} }, { $sort : {total : -1} } );
结果 -- “total“的降序显示,升序显示的话用$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“的数据根据”hosting“分组
> db.website.aggregate( { $match : {hosting : "aws.amazon.com"} }, { $group : { _id : "$hosting", total : { $sum : 1 } } } );
输出:
{ "result" : [ { "_id" : "aws.amazon.com", "total" : 4 } ], "ok" : 1 }
详细例子请参照官方手册 MongoDB Aggregation guide
3.把分组数据导出成CSV或者JSON格式文件
通过函数mongoexport来实现
3.1 将分组的结果保存到变量中,本例变量为”groupdata“
> var groupdata = db.website.aggregate( { $group : {_id : "$hosting", total : { $sum : 1 }} }, { $sort : {total : -1} } );
3.2 将”groupdata.result“插入到新集合中
> db.websitegroup.insert(groupdata.result); > 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
CSV文件中数据
group.csv _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
group.json文件内容
group.json { "_id" : "aws.amazon.com", "total" : 4 } { "_id" : "cloud.google.com", "total" : 2 } { "_id" : "godaddy.com", "total" : 1 } { "_id" : "hostgator.com", "total" : 3 }
相关推荐
MongoDB权威指南:MongoDB:The Definitive Guide第一版 第二版 中、英文4本合集
《Windows上安装MongoDB:完整步骤详解.pdf》是一份适用于Windows操作系统的MongoDB安装教程,适用于初学者和有经验的开发人员。本教程包括完整的步骤详解,可帮助您快速掌握MongoDB的安装配置,包括环境变量设置,...
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集
MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable generalpurpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...
文档层使用MongoDB:registered:有线协议,允许通过现有的MongoDB:registered:客户端绑定使用MongoDB:registered:API。 所有持久数据都存储在FoundationDB键值存储中。 文档层实现了MongoDB:registered:API(v ...
描述 PM2模块可自动监视mongodb的生命体征: 查询,输入,更新,删除 连接数 已用存储空间 网络速度(输入和输出) 代表名称和状态 pm2-mongodb ... pm2 set pm2-mongodb:ip 42.42.42.42 (我的mongod
NULL 博文链接:https://study121007.iteye.com/blog/2307592
Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar
mongodb人偶模块 目录 概述 从OS存储库或从MongoDB社区/企业存储库在RHEL / Ubuntu / Debian上安装MongoDB。 模块说明 ...如果只想使用默认选项安装服务器,则可以运行include mongodb::server 。
Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for ...
mongoDB:MongoDB备忘单
羽毛mongodb 用于数据库适配器,使用用于。 $ npm install --save mongodb feathers-mongodb ... connect ( 'mongodb://localhost:27017/feathers' ) . then ( client => { app . use ( '/messages' ,
安装$ npm install mqemitter-mongodb --save例子var mongodb = require ( 'mqemitter-mongodb' )var mq = mongodb ( { url : 'mongodb://127.0.0.1/mqemitter?auto_reconnect'} )var msg = { topic : 'hello world'...
docker run -d --name mongodb -p 27017:27017 -v /data/mongodb:/var/lib/mongodb anapsix/mongodb 作为客户: docker run -it --rm anapsix/mongodb mongo --help 配置 您可以像通常那样通过命令行传递配置选项...
spring-data-mongodb:1.1.0 java:1.8 log4j:1.2.16 junit:4.12 commons-logging:1.1.1 maven:3 注意:spring的不同版本与mongodb结合可能会有问题,自测:spring4.2.1结合mongodb会报错:**springframework.core....
Written by current and former members of the MongoDB team, the third edition is updated for MongoDB 4.0. You’ll find substantial updates on querying, indexing, aggregation, replica sets, ops manager,...
是一个数据分析和可视化中间件,旨在帮助您快速构建自定义的嵌入式数据分析应用程序。... "dsn" : "mongodb://localhost:27017/joola" } } } } 你们都准备好了。 为确保它正常工作,请运行一个节点并监视日志以查
用于DartORM的MongoDB适配器。 用法示例 import 'package:dart_orm/dart_orm.dart' as ORM; import 'package:dart_orm_adapter_mongodb/... 'mongodb://$ mongoUser :$ mongoPass @127.0.0.1/$ mongoDBName ' ); awa
MoniDB Yii 2扩展该扩展为提供了集成。 有关许可证信息,请... 'components' => [ 'mongodb' => [ 'class' => '\yii\mongodb\Connection' , 'dsn' => 'mongodb://localhost:27017/mydatabase' , 'options' => [ "user
Stitch SDK弃用通知Stitch SDK功能已移至Realm SDK。 使用之一,可以将使用... 安卓将以下内容添加到您的应用程序模块的build.gradle中: implementation ' org.mongodb:stitch-android-sdk:4.7.0 ' 还将以下内容添加到