1
def find(query: Document, projection: Document, collectionName : MongoCollection[Document]) : Document = {
var previousDoc : Document = Document()
/*var future = collectionName.find(query).projection(projection).toFuture()*/
try {
collectionName.find(query).projection(projection).subscribe(
(data: Document) => previousDoc = data,
(error: Throwable) => println("error"),
() => println("Completed")
)
} catch {
case x:Exception => throw new MongoCustomException(x)
}
//Await.result(future, Duration.Inf)
Thread.sleep(1000)
previousDoc
}
這是我的代碼片段,如果我沒有使用等待或線程,我會得到空文檔。在從mongodb獲取數據之前它正在退出。我想在Scala中同步運行此過程,而不使用Await和Thread方法。我想使用scala驅動程序在mongodb上執行查找操作
你正在使用哪一個mongo客戶端? – Abhi
你可以在你從查詢中獲得的'Observable'上調用'toFuture',然後對它進行模式匹配以得到值。 – sebszyller
您可以查看http://reactivemongo.org/releases/0.11/documentation/tutorial/find-documents.html – cchantep