0

我有一个 Ruby 关联类,如下所示:

class Association
  include Mongoid::Document
  field :issued, type: Integer
  field :lifetime, type: Integer
end

我想删除我的集合中的所有文档 where doc.issued + doc.lifetime > Time.now. 我在网上找到了以下语法:

Association.delete_all(["issued + lifetime > ?", Time.now.to_i])

我无法让它工作,我收到以下错误:

TypeError: can't convert Symbol into Integer
    from /var/lib/gems/1.9.1/gems/mongoid-3.1.0/lib/mongoid/persistence.rb:298:in `[]'
    from /var/lib/gems/1.9.1/gems/mongoid-3.1.0/lib/mongoid/persistence.rb:298:in `delete_all'
    from (irb):77
    from /usr/bin/irb:12:in `<main>'

有人能告诉我这种语法有什么问题或建议不同的语法吗?

4

1 回答 1

1

delete_allwhere一样,需要单个字段的条件列表,例如:

Association.delete_all(issued: 10)

在您的情况下,您可以使用 Javascript。

Association.for_js("(this.issued + this.lifetime) > ti", ti: Time.now.to_i).delete_all

注意:性能不会很好,因为条件将在每个文档上进行测试。

于 2013-02-22T18:38:17.647 回答