Mongodb新增的聚合方法及其Java客户端
2015-09-13 00:01
465 查看
Aggregation Framework Reference¶
http://cn.docs.mongodb.org/manual/reference/aggregation/#aggregation-framework-reference
Java Driver and Aggregation Framework¶
http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/Let’s use a simple example to demonstrate how the aggregation helper works. Suppose I am using MongoDB to store my employee’s travel expenses. I’ve created a collection named expenses,
which store individual expenses by employee and by department. Here’s a sample document:
{ "_id" : ObjectId("503d5024ff9038cdbfcc9da4"), "employee" : 61, "department" : "Sales", "amount" : 77, "type" : "airfare" }
I am auditing three departments: Sales, Engineering and Human Resources. I want to calculate each department’s average spend on airfare. I’d like to use the Aggregation Framework for the audit, so I think of the operation in terms of a pipeline:
Operation: Match documents where type = "airfare";
then pipe into
Operation: Pass only the department and the amount fields
through the pipeline; then pipe into
Operation: Average the expense amount, grouped by department.
I will use the aggregation operators $match, $project and $group to
perform each operation. Individual aggregation operations can be expressed as JSON objects, so I can think of my pipeline in JSON as:
First operation:
$match: { type: "airfare"}
Piped into:
$project: { department: 1, amount: 1 }
Piped into:
$group: { _id: "$department", average: { $avg: "$amount" } }
也就是说,运行以下命令:
db.expenses.aggregate({ $match:{type: "airfare"} $project:{_id:0,department: 1, amount: 1} $group:{_id: "$department", average: { $avg: "$amount" }} })
Java 实现:
// create our pipeline operations, first with the $match DBObject match = new BasicDBObject("$match", new BasicDBObject("type","airfare") );
// build the $projection operation DBObject fields = new BasicDBObject("department", 1); fields.put("amount",1); fields.put("_id", 0); DBObject project = new BasicDBObject("$project", fields );
// Now the $group operation DBObject groupFields = new BasicDBObject( "_id", "$department");groupFields.put("average", new BasicDBObject( "$avg", "$amount"));DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation AggregationOutput output = collection.aggregate( match, project, group );
Aggregations are executed as database commands in MongoDB. These commands embed the results of the aggregation task in an object that also contains additional information about how the command was executed. The return value of aggregate() is an instance of the AggregationOutput class, which provides assessors to this information.
public Iterable<DBObject> results() public CommandResult getCommandResult public DBObject getCommand()
Let’s take a look at the results of my audit:
System.out.println(output.getCommandResult());
{ "serverUsed" : "/127.0.0.1:27017" , "result" : [ {"_id" : "Human Resources","average": 74.91735537190083}, {"_id" : "Sales" , "average" : 72.30275229357798}, {"_id" : "Engineering" , "average" : 74.1} ], "ok" : 1.0 }
相关文章推荐
- java操作mongodb(聚合函数)-分组找出每组的最大值
- MongoDB简单概述
- MongoDB学习笔记<两>
- MongoDB中添加管理员和普通用户以及认证的方法
- MongoDB学习笔记
- mongodb学习一 关于 下载 安装 以及增删改查 还有一些简单的命令
- MongoDB MapReduce速度提升20倍的优化宝典
- mongodb 聚合详解
- MongoDB Spring实现(聚合查询)
- mongodb副本集群搭建
- 如何配置 MongoDB 副本集
- 结合MongoDB开发LBS应用
- MongoDB删除字段和部分字段查询
- Mongodb数据 导入导出数据 导出工具mongoexport和导入工具mongoimport介绍
- MongoDB整合Spring 详细讲解(含代码)
- mongodb对日期范围查询
- linux,centous 安装 mongodb
- MongoDB的地埋空间数据存储、空间索引以及空间查询
- MongoDB慢日志分析
- 将MySQL数据导入MongoDB