2013-10-15 2 views
0

어떻게 Casbah에 DBObject에 재귀 적으로 추가 한 다음 각 목록 요소가 추가 된 단일 MongoDBObject을 리턴 할 수 있습니까? (가) 아래 컴파일이나 직장,하지만 내가 위의 식으로 뭔가를 시도했을 때 내 원하는 코드재귀 적으로 MongoDBObject를 작성하십시오

def foo(pairs: List[(String, Int)]): List[MongoDBObject] = { 
    def go(ps: List[(String, Int)], acc: List[MongoDBObject]): List[MongoDBObject] = 
     ps match { 
     case x :: xs if(x._1 == "BAD") => go(xs, acc) 
     case x :: xs => go(xs, MongoDBObject(x._1 -> x._2) :+ acc) /* error line */ 
     case Nil => acc 
    } 
} 

val pairsList: List[MongoDBObject] = foo(getPairs()) // assume getPairs() is defined 
val builder = // do something to convert pairsList -> MongoDBObject (maybe a fold?) 
val results = collection.find(builder) 

에게 보여주기 위해 의도하지 않는

주, 난 내 두 번째 경우 문에서 다음 컴파일시 에러를 보았다 . 당신이 목록에 새 개체를 추가하려면

[myApp] $ compile 
[info] Compiling 1 Scala source to ... 
[error] c:\development\myApp\Test.scala:85: overloaded method value apply with alternatives: 
[error] [A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply) < 
: String, B(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)](e 
lems: List[(A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply), 
B(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply))])com.mongo 
db.casbah.commons.Imports.DBObject <and> 
[error] [A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply) < 
: String, B(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)](e 
lems: (A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply), B(in 
method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply))*)com.mongodb.ca 
sbah.commons.Imports.DBObject 
[error] cannot be applied to (com.mongodb.casbah.query.Imports.DBObject with com.mongodb.casbah.query.dsl.QueryExpressionObject) 
[error] go(xs, acc :+ MongoDBObject(elemMatch)) 
[error]                    ^
[error] one error found 

답변

2

이 그것을 수행해야합니다

MongoDBObject(x._1 -> x._2) :: acc 
+0

를 내 경우, 나는 (XS, MongoDBObject (elemMatch) : ACC)를 '이동'시도 하지만 위와 같은 오류가 발생했습니다. 'elemMatch'는 당신이 이전에 그것을 만들 수 있도록 도와 주므로 유효합니다. –

+0

'MongoDBObject ("a"-> "b") :: List (MongoDBObject ("c"-> "d"))'나를 위해 작동합니다. 아마도 약간의 설명이 필요할 것입니다. – Ross

관련 문제