2016-11-15 17 views
1

이 샘플 데이터입니다 (EXPIREDATE는 선택 사항입니다) :Reactivemongo Aggregation Framework에서 "Max"그룹 기능에서 여러 개의 키를 지정하는 방법은 무엇입니까?

db.collection_name.aggregate([ 
{ 
    $match : {"userId" : "1"} 
}, 
{ 
    $group : { 
     "_id" : "$appId", 
     "docs" : { 
      $max : { 
       "createdDate" : "$createdDate", 
       "expireDate" : "$expireDate" 
      } 
     } 
    } 
} 
]) 

는 샘플 데이터에 집계 함수를 실행하려면 : 이것은 통합 프레임 워크를 reactivemongo을 번역 할 집계 함수가

{"userId":"1","appId":"1","createdDate":10} 
{"userId":"1","appId":"1","createdDate":5,"expireDate":30} 
{"userId":"1","appId":"1","createdDate":12,"expireDate":20} 
{"userId":"1","appId":"1","createdDate":12,"expireDate":5} 

(mongo 쉘 3.2.9와 함께) 결과는 다음과 같습니다.

{ "_id" : "1", "docs" : { "createdDate" : 12, "expireDate" : 20 } } 

이 집계 함수를 reactmong으로 변환하려고하면 o, 그룹 함수 "Max"는 하나의 문자열 만 매개 변수로 사용하므로 "createdDate"와 "expireDate"를 모두 입력하는 방법을 알지 못합니다.

col.aggregate(
    Match(BSONDocument("userId" -> "1")), 
    List(Group(BSONString("$appId"))("docs" -> Max("createdDate"))) 
) 

이 사람이 어떻게 "최대"기능으로 "EXPIREDATE"를 추가하는 방법을 말해 줄래 : 여기에 지금까지 알아낼 무엇인가?
reactmongo 0.11을 사용하고 있으며 0.12로 업그레이드하는 것은 옵션이 아니라는 점에 유의하십시오.

답변

1

reactivemongo.api.commands.AggregationFramework.GroupFunction.apply을 사용하여 그룹 기능에 대한 사용자 지정 호출을 만들 수 있습니다. Here is the dochere is the source where I found it.

GroupFunction("$max", BSONDocument("createdDate" -> "$createdDate", "expireDate" -> "$expireDate")) 

대신 사용 Max이 기능은, 그래서 당신의 코드가된다 :

col.aggregate(
    Match(BSONDocument("userId" -> "1")), 
    List(Group(BSONString("$appId"))("docs" -> GroupFunction("$max", BSONDocument("createdDate" -> "$createdDate", "expireDate" -> "$expireDate")))) 
) 

col.BatchCommands.AggregationFramework.GroupFunction를 가져올 것을 잊지 마십시오. 당신이 원하는 경우, 매개 변수로 어떤 BSONDocument 소요 더 일반적인 기능을 정의 할 수 있습니다 물론

:

def BetterMax(doc: BSONDocument) = GroupFunction("$max", doc) 
+0

감사 파블이 나를 위해 완벽하게 작동합니다! –