讀http://reactivemongo.org/releases/0.11/documentation/tutorial/consume-streams.html添加有此代碼返回文檔,因爲它們是使用Scala reactivemongo
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.libs.iteratee._
import reactivemongo.bson.BSONDocument
import reactivemongo.api.collections.bson.BSONCollection
def processPerson1(collection: BSONCollection, query: BSONDocument): Future[Unit] = {
val enumeratorOfPeople: Enumerator[BSONDocument] =
collection.find(query).cursor[BSONDocument].enumerate()
val processDocuments: Iteratee[BSONDocument, Unit] =
Iteratee.foreach { person =>
val lastName = person.getAs[String]("lastName")
val prettyBson = BSONDocument.pretty(person)
println(s"found $lastName: $prettyBson")
}
enumeratorOfPeople.run(processDocuments)
}
運行被定義爲:「驅動iteratee消耗枚舉的輸入,在輸入的末尾添加Input.EOF 。返回結果或異常。' from https://www.playframework.com/documentation/2.5.1/api/scala/index.html#play.api.libs.iteratee.Enumerator這是否意味着如果將新文檔添加到數據庫中,則需要再次調用'processPerson1',以便該行可以運行以便返回。
我只是想返回文檔作爲他們被添加到數據庫而無需重新調用相同的代碼。可能「不是很好」的解決方案是隻是將enumeratorOfPeople.run(processDocuments)
包裝在預定的線程中,但接收所有文檔的問題依然存在,我只想返回還沒有返回的文檔
不特定於ReactiveMongo。你應該看看capoed系列。 – cchantep
@cchantep你的意思是'capped collections'? –