2016-05-23 64 views
0

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)包裝在預定的線程中,但接收所有文檔的問題依然存在,我只想返回還沒有返回的文檔

+0

不特定於ReactiveMongo。你應該看看capoed系列。 – cchantep

+0

@cchantep你的意思是'capped collections'? –

回答

0

我使用查詢創建了封頂的mongo集合:

db.createCollection( 「cappedCollection」,{ 「封端」: 「真」, 「autoIndexId」: 「真」, 「大小」:4096, 「最大值」:10})

然後在使用find查詢時使用選項:

.options(QueryOpts().tailable.awaitData) - 更多詳細信息:Play + ReactiveMongo: capped collection and tailable cursor

當一個新的文檔添加到集合run方法將返回最新添加的文檔。

相關問題