Forum: Ruby on Rails Best way to loop over mongo collection?

Posted by Edward Stembler (ejstembler)
on 2013-02-26 17:22
I have a mongo collection in Rails where I use it this manner:

collection.find(selector, options).to_a

I have around 47K rows of data, and it seems very slow doing it this
way.

Should I be using the cursor instead of converting to an array?  If so,
can anyone point me to the usage pattern? Or, is using to_a fine?  Just
wondering if converting to an array is too slow.
Posted by Jordon Bedwell (Guest)
on 2013-02-26 17:29
(Received via mailing list)
On Tue, Feb 26, 2013 at 10:22 AM, Edward Stembler <lists@ruby-forum.com> 
wrote:
> I have a mongo collection in Rails where I use it this manner:
>
> collection.find(selector, options).to_a
>
> I have around 47K rows of data, and it seems very slow doing it this
> way.
>
> Should I be using the cursor instead of converting to an array?  If so,
> can anyone point me to the usage pattern? Or, is using to_a fine?  Just
> wondering if converting to an array is too slow.

How about you tell us what ODM you are using?
Posted by Edward Stembler (ejstembler)
on 2013-02-26 17:32
Jordon Bedwell wrote in post #1099153:
> On Tue, Feb 26, 2013 at 10:22 AM, Edward Stembler <lists@ruby-forum.com>
> wrote:
>> I have a mongo collection in Rails where I use it this manner:
>>
>> collection.find(selector, options).to_a
>>
>> I have around 47K rows of data, and it seems very slow doing it this
>> way.
>>
>> Should I be using the cursor instead of converting to an array?  If so,
>> can anyone point me to the usage pattern? Or, is using to_a fine?  Just
>> wondering if converting to an array is too slow.
>
> How about you tell us what ODM you are using?

I'm not sure what ODM stands for. I'm using the standard ruby driver for 
mongo; not MongoMapper or Mongoid.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.