검색시 sort 여러가지 옵션으로 검색하게 되면 

결과값이 많이 나오거나 많은 양을 데이터를 검색시 메모리 오류가 난다. 

 

 

 

 메모리를 늘리는 방법도 가능하다리미트를 주는방식도 가능하다 

 

db.getCollection('dogdrip').find({}).sort( {"date" : -1}).limit(1 

 

 

이러한 일련의 과정을 한번에 해결해주는 방법이 있다. 

 

몽고디비에 데이터 집계를 위한 프레임워크가 있다.  Aggregate라는것이다. 

 

 

 

이렇나 파이프라인 구조로 데이터를 줄여가며 보여주게 된다. 

 

파이프 라인구성 문법들은 다음과같다. 

 

 

 

 

정말 많다 레벨을 구성하여 데이터를 줄여나갈수있다. 

 

이에 따른 옵션도 굉장히 많다. 

 

 

 

 

공식문서로 확인할수있음 

 

이것을 토대로 위에 소트한 내용을 다시 한번 하게되면 

 

 

 

 

이런식으로 구성할수있다. 

 

좀더 쉽게  집계할 수잇는 방법이 있는거같다 

 

Java jdbc  해보자 어렵지않다 그대로 써주면된다. 

 

 

AggregationOutput com.mongodb.DBCollection.aggregate(List<? extends DBObject> pipeline) 

Deprecated. Use aggregate(List, AggregationOptions) instead 

Method implements aggregation framework. 

Parameters: 

pipeline operations to be performed in the aggregation pipeline 

Returns: 

the aggregation's result set 

@mongodb.driver.manual 

core/aggregation-pipeline/ Aggregation 

@mongodb.server.release 

2.2 

 

 

TRICK TO CONVERT MONGO SHELL QUERY INTO EQUIVALENT JAVA OBJECTS 

Earlier we completed with Mongodb aggregation , examples and shell queries.Still this time I have something thing to share.We learned aggregation queries and its bit easier while writing in to the mongo shell because initially we learned it via shell.But while implementing some application we need to convert it into the appropriate programming language. 

This article is written aiming to the java developers as its quite complex in java while creating large aggregation query comparing to node.js or python which is much similar to shell query in terms of syntax. 

But after reading this article it would be easier to write aggregation queries in java also.Considering one example fromdocs.mongodb.org we will see how to do it. 

Below is a simple dataset for the example. 

{  "_id" : ObjectId("503d5024ff9038cdbfcc9da4"), 
   "employee" : 1, 
   "department" : "Sales", 
   "amount" : 77, 
   "type" : "airfare" 
} 

Above query illustrate the $match operator example using aggregation function. This query will display all the documents that have type = local. I have simplified this query usingkey-value (here we separate key-value by : ) partition as you can see in above image.Now you know that how to decompose aggregation query into key-value pair and value into the further key-value pair and so on. 

Now coming to the java part, java driver uses theBasicDBObject class to construct the aggregation query.One of the BasicDBObject’s constructor contains two parameters as below. 

new BasicDBObject(String key , Object value); 

Now mapping “String key” with the key in shell query and “Object value” with the value in shell query. 

After this now i’ll write java code so you can clearly understand how its done. 

DBObject match = new BasicDBObject("$match", new BasicDBObject("type""local")); 

In few cases you can have multiple details under value part separated by comma ( , ) as below,So we can use.append(String key , Object value) method. 

SHELL :  
db.aggregationExample.aggregate( 
       {$project : { department : 1 , amount : 1 }} 
); 
 

JAVA :  
DBObject project = new BasicDBObject("$project" ,  
       new BasicDBObject("department" , 1).append("amount" , 1)); 

Final Example 

public class JavaAggregation { 
public static void main(String args[]) throws UnknownHostException{ 
MongoClient mongo = new MongoClient(); 
DB db = mongo.getDB("pingax"); 
 

DBCollection coll = db.getCollection("aggregationExample"); 
 

/* 
MONGO SHELL : db.aggregationExample.aggregate( 
{$match : {type : "local"}} , 
{$project : { department : 1 , amount : 1 }} , 
{$group : {_id : "$department" , average : {$avg : "$amount"} } } , 
{$sort : {"amount" : 1}} 
); 
*/ 
DBObject match = new BasicDBObject("$match", new BasicDBObject("type", "local"));  
 

DBObject project = new BasicDBObject("$project", new BasicDBObject("department", 1) 
.append("amount", 1)); 
 

DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$department") 
.append("avrage", new BasicDBObject("$avg", "$amount"))); 
 

DBObject sort = new BasicDBObject("$sort", new BasicDBObject("amount", 1)); 
 

AggregationOutput output = coll.aggregate(match,project,group,sort); 
 

for (DBObject result : output.results()) { 
System.out.println(result); 
} 
} 
} 
 

OUTPUT :  
{ "_id" : "Development" , "avrage" : 111.0} 

 

 

 

 

 

 

 

 

 

 

 

 

 


'오락기 > mongoDB' 카테고리의 다른 글

query rdb와 동일하게 할려면  (0) 2018.08.17
sort lime skip  (0) 2018.08.17
distinct + java api  (0) 2018.08.17
upset + java api  (0) 2018.08.17
JDBC 연동  (0) 2018.08.17

+ Recent posts