2017-11-09 11 views
7

나는 Aggregation aggregation을 매개 변수로받는 함수를 가지고있다.Spring Mongo>리스트를 얻는 방법 집계에서 집계 연산

AggregationOperationaggregation에서 받고 싶습니다. 그것을 할 방법이 있습니까?

public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) { 
    // How to get list operation aggregation there? 
    listOperation.push(Aggregation.match(c)); 
    return Aggregation 
      .newAggregation(listOperations); 
} 

내 목적은 내 사용자 지정 MatchAggregation 또 다른 Aggregation 새로운 기능입니다.

+0

같은 뭔가? – chridam

+0

안녕하세요 chridam, 동적 기준을 추가하여 매개 변수로받을 때 결과를 필터링 할 때 사용하고 싶습니다. query.addCriteria (... myCriteria)와 같은 방식으로) –

답변

4

보호 작업 필드에 액세스하기 위해 집계를 하위 클래스 화하여 사용자 정의 집계 구현을 만들 수 있습니다.

이`newCustomAggregation` 기능을 사용할 계획이 어떻게
public class CustomAggregation extends Aggregation { 
     List<AggregationOperation> getAggregationOperations() { 
     return operations; 
    } 
} 

public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) { 
    CustomAggregation customAggregation = (CustomAggregation) aggregation; 
    List<AggregationOperation> listOperations = customAggregation.getAggregationOperations(); 
    listOperations.add(Aggregation.match(c)); 
    return Aggregation .newAggregation(listOperations); 
} 
+0

이것은 질문에 대답하지 않습니다. 질문은'Aggregation' 인스턴스에서'AggregationOperation' 목록을 가져 오는 것입니다. 이 솔루션은'CustomAggregation' 인스턴스에서만'AggregationOperation' 목록을 가져옵니다. 다른 모든'Aggregation'의 경우 그냥'ClassCastException'을 던집니다. 나는 그것에 대해 많은 감각을 보지 못한다. – jannis

+0

@jannis 이미 언급했듯이'Aggregation' 클래스에서'operations' 필드를 얻을 방법이 없습니다. op가'aggregated' 클래스를 서브 클래스 화하여'protected'operations' 필드에 접근하고 getter를 통해 필드를 공개 할 수있는 곳에서 답을 제공했습니다. 어떤 부분이 아직 명확하지 않은지 알려주십시오. – Veeram

+0

나는 명확하지 않다는 말은 아니다. 나는 그것이 질문에 대답하지 않는다고 말하고있다. – jannis

2

Aggregation의 속성은 operations이며 모든 적용 작업을 Aggregation에 제공합니다.

protected List<AggregationOperation> allOperations = aggregation.operations ; 

은 모든 적용된 작업을 제공합니다.

+2

A) 정적이 아닙니다. B) 보호 받고있어. – jannis

+0

고맙습니다. 업데이트 내 대답 –

+0

나는이 속성을 얻을 수있는 방법이 없습니다 .. 거기에 어떤 방법이 있습니까? –

4

짧은 대답 : 아니요, 할 방법이 없습니다. operationsAggregation 클래스의 보호 속성입니다 -

Aggregation 예를 외부에서 AggregationOperation 목록을 가져 오는 어떤 '쉬운'방법은 없습니다.

리플렉션을 사용하면 쉽게 얻을 수 있지만 이러한 코드는 깨지기 쉽고 유지 관리 비용이 비쌉니다. 아마도이 건물을 보호해야 할 충분한 이유가있을 것입니다. 이것을 Spring-MongoDB's JIRA에서 물어볼 수 있습니다. 나는 이것에 대한 대안적인 접근이 있다고 가정한다.

매개 변수로 AggregationOperation 컬렉션을 가져 오는 방법을 변경할 수도 있지만이 솔루션을 사용할 수있는 경우 게시물에 정보가 너무 적습니다.