cursor.next cusor.hasNext sql like statements

24 views
Skip to first unread message

Sunil Pasumarthi

unread,
Jun 20, 2016, 7:10:15 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
how to iterate over documents like batches? Is it wrong doing so??

Cédric Chantepie

unread,
Jun 20, 2016, 7:26:13 AM6/20/16
to ReactiveMongo - http://reactivemongo.org

Sunil Pasumarthi

unread,
Jun 20, 2016, 7:57:17 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
i am getting "cursor producer" whenever i am using cursor() function on collection.find()

are there any examples that could guide me?? 

Cédric Chantepie

unread,
Jun 20, 2016, 8:08:40 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
I suggest you first read the tutorial, try some code, and if your code doesn't work you can paste that there so you could get more helpful answer.

Sunil Pasumarthi

unread,
Jun 20, 2016, 10:34:32 AM6/20/16
to ReactiveMongo - http://reactivemongo.org

    val db = connection.apply("dummy")
    val collection = db.collection[BSONCollection]("dummy")

    val cursor = collection.find(BSONDocument()).options(QueryOpts(batchSizeN = 3)).cursor[BSONDocument]()

    cursor.foldWhile(List.empty[BSONDocument])(
      { (list, bsonDoc) =>
        println(BSONDocument.pretty(bsonDoc))
        println("batch finished")
        Cursor.Cont(list)
      },
      { (list, err) =>
        Cursor.Fail(err)
      }
    )


data in mongodb is {"_id":"a"}, {"_id": "b"} through {"_id":"z"}

i tried above code and it is resulting "batch finished" at every document cursor has fetched, inspite of specifying query opts as 3.
i want to print "batch finished" for every 3 documents like
{"_id": "a"}
{"_id": "b"}
{"_id": "c"}
batch finished"
{"_id": "d"}
...
{"_id": "z"}

Sunil Pasumarthi

unread,
Jun 20, 2016, 10:52:31 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
i found this, but cant figure out when to call Cursor.Done

    val db = connection.apply("dummy")
    val collection = db.collection[BSONCollection]("dummy")

    val cursor = collection.find(BSONDocument()).options(QueryOpts(batchSizeN = 3)).cursor[BSONDocument]()

    cursor.foldBulks(List.empty[BSONDocument])(
      { (list, bsonDocIter) =>
        bsonDocIter.foreach { bsonDoc =>

Cédric Chantepie

unread,
Jun 20, 2016, 10:58:42 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
You misunderstand the meaning of the suc and err arguments. The suc is applied on each successfully found element, not at end (as in foldLeft in Scala collection).
If you don't need a specific error handling, you'd better use .fold.
If you want to process batch per batch, you need to use foldBulks.

Cédric Chantepie

unread,
Jun 20, 2016, 11:00:45 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
Done is a there if you want to stop processing prematurely, without error.

It looks like what you've done is re-implementing what can already be done using Cursor.collect.
Message has been deleted

Sunil Pasumarthi

unread,
Jun 20, 2016, 11:47:19 AM6/20/16
to ReactiveMongo - http://reactivemongo.org
I can achieve processing batch by batch using foldBulks.

As per your reply, there is no need of using Cursor.Done after processing all the batches. is my understanding correct?

------------
THANKS 

Cédric Chantepie

unread,
Jun 20, 2016, 12:54:42 PM6/20/16
to ReactiveMongo - http://reactivemongo.org
The fact of using batch is used anyway with cursor as long as the option is given. Your code is collecting over documents found using batch, with no partition logic per bulk/batch, so reimplementing it with foldBulk is useless for me.
Reply all
Reply to author
Forward
0 new messages