zoukankan      html  css  js  c++  java
  • Mongodb新增的聚合方法及其Java客户端

    Aggregation Framework Reference


    Java Driver and Aggregation Framework


    Let’s use a simple example to demonstrate how the aggregationhelper works. Suppose I am using MongoDB to store my employee’stravel expenses. I’ve created a collectionnamed 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 HumanResources. I want to calculate each department’s average spend onairfare. 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 documentswhere type = "airfare";then pipe into
    2. Operation: Pass onlythe department andthe amount fieldsthrough the pipeline; then pipe into
    3. Operation: Average theexpense amount,grouped by department.

    I will use the aggregation operators $match$project and $group toperform each operation. Individual aggregation operations can beexpressed as JSON objects, so I can think of my pipeline in JSONas:

    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 DBObjectmatch=newBasicDBObject("$match",newBasicDBObject("type","airfare")); 
      
       
      // build the $projection operation DBObjectfields=newBasicDBObject("department",1);fields.put("amount",1);fields.put("_id",0);DBObjectproject=newBasicDBObject("$project",fields); 
      
      // Now the $group operationDBObjectgroupFields=newBasicDBObject("_id","$department");groupFields.put("average",newBasicDBObject("$avg","$amount"));DBObjectgroup=newBasicDBObject("$group",groupFields); 
      
      // run aggregationAggregationOutputoutput=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
      }
      
      
      
  • 相关阅读:
    LeetCode 93. Restore IP Addresses
    LeetCode 92. Reverse Linked List II
    LeetCode 94. Binary Tree Inorder Traversal
    javaweb中重定向和请求转发(response.sendRedirect()和request.getRequestDispatcher(rul).forward(request,response)))的区别
    java关于jdbc的配置与使用步骤
    关于php中的include html文件的问题,为什么html可以在php中执行
    yii2 无法显示debug条的问题解决方法
    elasticsearch报错expected <block end>, but found BlockMappingStart解决方法
    sysctl -p 报错问题的解决方法
    yii2 Rbac使用yii命令一键建表
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276592.html
Copyright © 2011-2022 走看看