0

Reading http://reactivemongo.org/releases/0.11/documentation/tutorial/consume-streams.html have this code

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)
}

Run is defined as : 'drives the iteratee to consume the enumerator's input, adding an Input.EOF at the end of the input. Returns either a result or an exception.' from https://www.playframework.com/documentation/2.5.1/api/scala/index.html#play.api.libs.iteratee.Enumerator Does this mean that if a new document is added to the database the 'processPerson1' will need to be invoked again so that this line enumeratorOfPeople.run(processDocuments) can run in order for it be returned.

I just want to return the documents as their being added to DB without re-invoking same code. Possible 'not very good' solution is to to just wrap enumeratorOfPeople.run(processDocuments) in a scheduled thread but the problem of receiving all documents remains, I just want to return documents that have not yet been returned

blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

0

I created a capped mongo collection using query:

db.createCollection("cappedCollection", { "capped": "true", "autoIndexId ": "true", "size": 4096, "max": 10} )

Then using options when querying with find :

.options(QueryOpts().tailable.awaitData) - more detail: Play + ReactiveMongo: capped collection and tailable cursor

When a new document is added to collection the run method will return the latest added document.

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752