Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo query using mongoid in rails app causing cursor timeout error

I have a mongo query in my rails app that is timing out because the collection is huge.

FbCheckin.where(ext_fb_place_id: self.ext_fb_place_id).all

I've read from documentation that you can add a timeout option to prevent the cursor from timing out with the following message:

Moped::Errors::CursorNotFound: The operation: "GET MORE" failed with error

I've tried several ways including

FbCheckin.where(ext_fb_place_id: ext_fb_place_id, {:timeout=>false}).all

and

FbCheckin.find(ext_fb_place_id: ext_fb_place_id, {:timeout=>false}).all

but none of these prevent the cursor from timing out.

Does anyone know how I can make this query and gather all the FbCheckins without the cursor timing out beforehand?

Thanks

like image 733
Huy Avatar asked Feb 08 '13 00:02

Huy


2 Answers

What you want is to set the cursor timeout to false when you are querying mongodb.

Here is what you can do with mongoid 3:

FbCheckin.where(...).no_timeout.each do |fb_checkin|
  "do something with fb_checkin"
end
like image 65
Quentin Avatar answered Nov 12 '22 09:11

Quentin


Use 'no_cursor_timeout' option along with the find query while using Mongo Ruby Driver.

This will disable all cursor timeouts. By default MongoDB tries to kill all cursors which have been inactive for more than 10 mins.

More info can be found here.

like image 21
Vinu Joseph Avatar answered Nov 12 '22 08:11

Vinu Joseph