2012-02-01 1 views
0

나는 세 개의 객체Casbah 및 하위 집합을 사용하여 여러 수준의 개체를 관리하는 방법은 무엇입니까?

case class Metric(val name: String, val tags: Map[String, String]) 

case class Threshold(val metric: Metric, val critical: Long, val warning: Long) 

class Profile(val name: String, val thresholds: List[Threshold]) 

내가 몽고 DB 만 프로필 개체를 저장하는 계획을 가지고 있지만 스칼라 App에서 그들은 자신의 유형으로 표현되어야한다.

나는 같은 대한 부분 집합을 이용하고 있고

implicit val reader = ValueReader[Threshold]({ 
case metric(metric) ~ critical(critical) ~ warning(warning) => 
    new Threshold(metric, critical, warning) 
}) 
implicit val writer = { 
def f(threshold: Threshold): DBObject = 
    (metric -> threshold.metric) ~ (critical -> threshold.critical) ~ (warning -> threshold.warning) 
ValueWriter(f _) 
} 

가 어떻게에 지금 몽고에서 조회 할 수 있습니다 다음과 같은 성격의 정의? 이것에 대한 예가 있습니까?

답변

1

Integration test은 중첩 된 개체, 쿼리, 업데이트 등을 사용하는 좋은 예입니다.이 테스트의 일부는 documentation에도 흩어져 있습니다.

Mongo에서 읽으려면 모델의 모든 부분에 대한 독자가 필요합니다. 쿼리하거나 업데이트하려는 경우 작성자도 필요합니다. 스칼라 컴파일러는 필요한 암시 적 항목을 찾을 수없는 경우 오류를 발생시켜야합니다. 나는이 코드에서 가정의 부부를했습니다

object Profile { 
    val name = "name".fieldOf[String] 
    val thresholds = "thresholds".subset(Threshold).of[List[Threshold]] 

    // typical query: 
    def alarmsFor(metric: String) = 
    collection.find(thresholds elemMatch {t => 
     t.metric.where{_.name == metric} && t.critical > 10 
    }) map { 
     case name(n) ~ thresholds(t) => new Profile(n, t) 
    } 
} 

:

당신은 어떻게 Profile의를 조회 할

필드는 당신이 얻을 곳 t은 ( object Threshold에 정의되어
  • Threshold의 그것)
  • Threshold.metric 필드는 하위 집합 자체입니다 val metric = "metric".subset(Metric).of[Metric], 당신은 (나는 결국이 계획하지만) 버전 0.7.0의로 Map[String,T]에 대한 리더/라이터가 아직 없다는 것을 metric.where{_.name == metric}

주를 조회 할 수 있도록 - 당신은 (그것을 개발해야합니다 이 필드가 필요하면 Metric의 리더/라이터에서이 문제를 해결하십시오.

+0

가정은 공정하고 많은 것을 명확하게 만듭니다. 감사 – sheki