【问题标题】:Mongo query using mongoid in rails app causing cursor timeout error在 Rails 应用程序中使用 mongoid 的 Mongo 查询导致光标超时错误
【发布时间】:2013-02-08 00:03:35
【问题描述】:

我的 rails 应用程序中有一个 mongo 查询超时,因为集合很大。

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

我从文档中了解到,您可以添加 timeout 选项以防止光标超时并显示以下消息:

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

我尝试了几种方法,包括

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

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

但这些都不能阻止光标超时。

有谁知道我如何进行此查询并收集所有FbCheckins 而不会事先光标超时?

谢谢

【问题讨论】:

  • 这个discussion on the mongoid mailing list 可以提供一些有用的提示。
  • @PrakashMurthy 谢谢,我会看看这个
  • 您找到解决方案了吗?
  • @cortex 不,我只是写了一个循环来一次提取批次。

标签: ruby-on-rails mongodb mongoid


【解决方案1】:

您想要的是在查询 mongodb 时将光标超时设置为 false。

以下是您可以使用 mongoid 3 执行的操作:

FbCheckin.where(...).no_timeout.each do |fb_checkin|
  "do something with fb_checkin"
end

【讨论】:

  • 我遇到了和@Huy 一样的问题。在后台任务中,我在更新数据库时迭代 Object.all。更新来自挖掘网络数据,因此它在每个循环中都有一个获取延迟,以避免过于频繁的远程页面获取。这导致游标超时。 @Quentin 的解决方案解决了这个问题。我将代码更改为Object.all.no_timeout.each do |object|,它运行良好。
【解决方案2】:

在使用 Mongo Ruby 驱动程序时,将 'no_cursor_timeout' 选项与查找查询一起使用。

这将禁用所有光标超时。默认情况下,MongoDB 会尝试杀死所有处于非活动状态超过 10 分钟的游标。

更多信息可以在here找到。

【讨论】:

    【解决方案3】:

    默认情况下,mongoid 会杀死长时间查询,然后引发此错误

    您可以更改 mongoid.yml 中的 raise_not_found_error 选项以避免此错误

    例如:

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

    【讨论】:

    • "raise_not_found_error(true):当尝试通过不存在的 id 查找文档时,将引发 Mongoid::Errors::DocumentNotFound。设置为 false 时,对于相同的查询只会返回 nil。 "
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多