2016-06-05 2 views
1

은 봄 데이터에 사용되는 :

proj3={"$project": { 
     "comms" : 1, 
     "same" : { "$eq" : ["$comms.i" , "$max"]}, 
    "max" : 1, 
    "_id" : 1 
    } 
}; 

나는 그것을 알아낼 수 없습니다. 나는 또한이 스레드 읽어

{ "$project" : { "comms" : 1 , "max" : 1 , "_id" : 1}} 

:

BasicDBObject o3 = new BasicDBObject(); 
    o3.append("$eq", "[\"$comms.i\",\"$max\"]"); 
    Aggregation aggCount2 = newAggregation(project("comms", "max", "_id").andExpression("same", o3)); 
    logger.info(aggCount2.toString()); 

이 기록됩니다 것입니다 :

나는이 시도 Spring Data MongoDB - $eq within $project support을하지만, 포스터 포기 듯 대신 ExecuteCommand를 옵션을 사용 그것은 내가 가고 싶지 않은 길입니다.

이 코드를 Java Spring Data Mongodb에서 작동 시키려면 어떻게해야합니까?

답변

0

이것은 어떻게 해결 했는가? 가장 효율적인 방법은 아니지만 너무 많은 코드를 다시 작성하지 않아도 작동하는 것 같습니다.

첫째, 나는이 글의 답변을 보았다 : Blakes 세븐 집계 코드가 BasicDBObjects을 수 있도록 내가 특별한 사용자 정의 aggregationOperation 클래스를 사용하는 것이 제안 Aggregation Project Group By extracted day, month and year with Spring Data

:

public class CustomGroupOperation implements AggregationOperation { 
    private DBObject operation; 

    public CustomGroupOperation (DBObject operation) { 
     this.operation = operation; 
    } 

    @Override 
    public DBObject toDBObject(AggregationOperationContext context) { 
     return context.getMappedObject(operation); 
    } 
} 

다음, BasicDBObject에 원하는 프로젝트 코드를 포맷하면됩니다.

BasicDBList basicDBList = new BasicDBList(); 
basicDBList.add("$comms.i"); 
basicDBList.add("$max"); 

Aggregation aggCount2 = newAggregation(
     match(), 
     project(), 
     group(), 
     new CustomGroupOperation(new BasicDBObject("$project", 
       new BasicDBObject("comms", 1) 
         .append("max", 1) 
         .append("_id", 1) 
         .append("same", new BasicDBObject("$eq", basicDBList)))), 
     match(), 
     project(), 
     group(), 
     sort()); 

로거를 사용하면 자바 스크립트 코드의 형식이 올바른지 확인할 수 있습니다.

{ "$project" : { "comms" : 1 , "max" : 1 , "_id" : 1 , "same" : { "$eq" : [ "$comms.i" , "$max"]}}} 
관련 문제