2017-04-27 2 views
0

아래 집계 쿼리가 있습니다. 몽고 세계에 처음 온 사람입니다. 누군가가 이것을 동등한 JAVA 쿼리로 변환 할 수 있습니까?Mongo Aggregation to java

db['mnocollection'].aggregate([ 
    { $match : { $and : [ {"stId" : { "$gte" : 1 }}, {"stId" : { "$lte" : 
      410 }} ] } }, 
    {$sort: { "objId" : -1 }}, 
    {$group: 
      { 
       _id : "$objId", 
       "maxstId" : {$max: "$stId" }, 
       "idVal" : {$first : "$_id"} 
      }}, 
       {$project: { idVal : 1, _id : 0, maxstId : 1}}, 
      { $skip: 0 }, 
    { $limit: 500} 
    ]); 

나는 자바에 대한 작업을 수행했다.

AggregateIterable<Document> output=mongoCollection.aggregate(Arrays.asList(...)); 

답변

2

헬퍼 클래스의 모든 메소드를 정적으로 가져 와서 아래 코드를 사용하십시오.

import static com.mongodb.client.model.Accumulators.*; 
import static com.mongodb.client.model.Aggregates.*; 
import static java.util.Arrays.asList; 
import static com.mongodb.client.model.Sorts.*; 
import static com.mongodb.client.model.Filters.*; 
import static com.mongodb.client.model.Projections.*; 

Bson match = match(and(gte("stId", 1), lte("stId", 410))); 
Bson sort = sort(descending("objId")); 
Bson group = group("$objId", first("maxstId", "$stId"), first("idVal", "$_id")); 
Bson projection = project(fields(include("idVal", "maxstId"), excludeId())); 
Bson skip = skip(0); 
Bson limit = limit(500); 
AggregateIterable<Document> output = mongoCollection.aggregate(asList(match, sort, group, projection, skip, limit)); 
+0

게시 한 후 github ....에있는 문서를 사용하여 변환 할 수있었습니다. – ProgrammerBoy

관련 문제