程式扎記: [ MongoDB 文件 ] Getting Started - Data Aggregation with Java Driver

標籤

2015年12月21日 星期一

[ MongoDB 文件 ] Getting Started - Data Aggregation with Java Driver

Source From Here 
Overview 
MongoDB can perform aggregation operations, such as grouping by a specified key and evaluating a total or a count for each distinct group. Use the aggregate method to perform a stage-based aggregation. The aggregate method accepts as its argument an array of stages, where each stage, processed sequentially, describes a data processing step. 

Prerequisites 
The examples in this section use the restaurants collection in the test database. For instructions on populating the collection with the sample dataset, see Import Example Dataset. Follow the Connect to MongoDB step to connect to a running MongoDB instance and declare and define the variable db to access the test database. To begin, including the following import statements. 
  1. import com.mongodb.Block;  
  2. import com.mongodb.client.AggregateIterable;  
  3. import org.bson.Document;  
  4.   
  5. import static java.util.Arrays.asList;  
Group Documents by a Field and Calculate Count 
Use the $group stage to group by a specified key. In the $group stage, specify the group by key in the _id field. $group accesses fields by the field path, which is the field name prefixed by a dollar sign $. The $group stage can useaccumulators to perform calculations for each group. The following example groups the documents in the restaurants collection by the borough field and uses the $sum accumulator to count the documents for each group. 
  1. AggregateIterable iterable = db.getCollection("restaurants").aggregate(asList(  
  2.         new Document("$group"new Document("_id""$borough").append("count"new Document("$sum"1)))));  
  3.   
  4. iterable.forEach(new Block() {  
  5.     @Override  
  6.     public void apply(final Document document) {  
  7.         System.out.println(document.toJson());  
  8.     }  
  9. });  
The result set consists of the following documents: 
{ "_id" : "Missing", "count" : 51 }
{ "_id" : "Staten Island", "count" : 969 }
{ "_id" : "Manhattan", "count" : 10259 }
{ "_id" : "Brooklyn", "count" : 6086 }
{ "_id" : "Queens", "count" : 5656 }
{ "_id" : "Bronx", "count" : 2338 }

The _id field contains the distinct borough value, i.e., the group by key value. 

Filter and Group Documents 
Use the $match stage to filter documents. $match uses the MongoDB query syntax. The following pipeline uses $match to query the restaurants collection for documents with borough equal to "Queens" and cuisine equal to Brazilian. Then the $group stage groups the matching documents by the address.zipcode field and uses the $sum accumulator to calculate the count. $group accesses fields by the field path, which is the field name prefixed by a dollar sign $
  1. AggregateIterable iterable = db.getCollection("restaurants").aggregate(asList(  
  2.         new Document("$match"new Document("borough""Queens").append("cuisine""Brazilian")),  
  3.         new Document("$group"new Document("_id""$address.zipcode").append("count"new Document("$sum"1)))));  
  4.   
  5. iterable.forEach(new Block() {  
  6.     @Override  
  7.     public void apply(final Document document) {  
  8.         System.out.println(document.toJson());  
  9.     }  
  10. });  
The result set consists of the following documents: 
{ "_id" : "11377", "count" : 1 }
{ "_id" : "11368", "count" : 1 }
{ "_id" : "11101", "count" : 2 }
{ "_id" : "11106", "count" : 3 }
{ "_id" : "11103", "count" : 1 }

The _id field contains the distinct zipcode value, i.e., the group by key value. 

Additional Information 
In the Java Driver documentation, see aggregateAggregateIterableBlock; In the MongoDB Manual, see also Aggregation Quick Reference, the SQL to Aggregation Mapping Chart, and Aggregation Introduction

Supplement 
Getting Started - Remove Data with Java Driver 
Getting Started - Indexes with Java Driver

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!