Question

I am using mongomapper for the mongodb queries. I have checked that using My_Modeal.all() returns a cursor for the specific collection of documents.

I wanted to perform .each() on each document, but it turns out that before processing .each() this command takes really long time to process (40 seconds).

How can I process all documents, one by one without delays, just like in the MySQL? (select * from table)

Was it helpful?

Solution

You want the common ActiveRecord method find_each for this use case (and for other datastores). For example:

MyModeal.find_each {|object| do_something(object) }

Behind the scenes, MongoMapper is issuing a MongoDB find which returns an enumerable cursor. The find_each method will iterate through the cursor, yielding one MongoMapper object at a time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top