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