Mongo query using mongoid in rails app causing cur

2019-01-23 16:00发布

问题:

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

回答1:

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


回答2:

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.



回答3:

mongoid will kill long time query by default and then raise this error

you can change raise_not_found_error option in mongoid.yml to avoid this error

for example:

production:
  sessions:
   default:
     database: local
     hosts:
      - localhost:27017
     options:
       allow_dynamic_fields: true
       raise_not_found_error: false