2016-05-23 75 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方法将返回最新添加的文档。

相关问题