Mongodb新增的聚合方法及其Java客户端

Aggregation Framework Reference


Java Driver and Aggregation Framework


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:

  1. Operation: Match documents where type "airfare"; then pipe into
  2. Operation: Pass only the department and the amount fields through the pipeline; then pipe into
  3. 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:

  1. First operation:

    $match: { type: "airfare"}
  2. Piped into:

    $project: { department: 1, amount: 1 }
  3. 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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值