Mongoid命名范围比较同一文档中的两个时间字段

时间:2021-11-15 20:05:38

I need to create a named scope in Mongoid that compares two Time fields within the same document. Such as

我需要在Mongoid中创建一个命名范围,用于比较同一文档中的两个时间字段。如

scope :foo, :where => {:updated_at.gt => :checked_at}

范围:foo,:where => {:updated_at.gt =>:checked_at}

This obviously won't work as it treats :checked_at as a symbol, not the actual field. Any suggestions on how this can be done?

这显然不会起作用,因为它将checked_at视为符号,而不是实际字段。有关如何做到这一点的任何建议?

Update 1

更新1

Here is my model where I have this scope declared, with a lot of extra code stripped out.

这是我的模型,我声明了这个范围,删除了许多额外的代码。

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  field :checked_at, :type => Time

  scope :unresolved, :where => { :updated_at.gt => self.checked_at }
end

This gives me the following error:

这给了我以下错误:

'<class:User>': undefined method 'checked_at' for User:Class (NoMethodError)

' ':用户的未定义方法'checked_at':Class(NoMethodError) :user>

3 个解决方案

#1


7  

As far as I know, mongodb doesn't support queries against dynamic values. But you could use a javascript function:

据我所知,mongodb不支持对动态值的查询。但你可以使用javascript函数:

scope :unresolved, :where => 'this.updated_at >= this.checked_at'

To speed this up you could add an attribute like "is_unresolved" which will be set to true on update when this condition is matched ( and index that ).

为了加快这一速度,你可以添加一个像“is_unresolved”这样的属性,当这个条件匹配时(和索引那个),它将在更新时设置为true。

#2


3  

scope :foo, :where => {:updated_at.gt => self.checked_at}

For example, this will work:

例如,这将起作用:

scope :foo, where(:start_date.lte=>Date.today.midnight)

#3


1  

Not sure if you'll like this method, it's not the best, but it should work.

不确定你是否喜欢这种方法,它不是最好的,但应该有效。

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  field :checked_at, :type => Time

  scope :unresolved, lambda{ |user| where(:updated_at.gt => user.checked_at) }
end

You call it with User.unresolved(my_user_object) It seems now after rereading your post that this probably won't do what you want. If this is true, then you will probably need to use MapReduce or possibly Baju's method (have not tested it)

你用User.unresolved(my_user_object)调用它现在看起来重新阅读你的帖子后,这可能不会做你想要的。如果这是真的,那么你可能需要使用MapReduce或者可能需要使用Baju的方法(尚未测试过)

#1


7  

As far as I know, mongodb doesn't support queries against dynamic values. But you could use a javascript function:

据我所知,mongodb不支持对动态值的查询。但你可以使用javascript函数:

scope :unresolved, :where => 'this.updated_at >= this.checked_at'

To speed this up you could add an attribute like "is_unresolved" which will be set to true on update when this condition is matched ( and index that ).

为了加快这一速度,你可以添加一个像“is_unresolved”这样的属性,当这个条件匹配时(和索引那个),它将在更新时设置为true。

#2


3  

scope :foo, :where => {:updated_at.gt => self.checked_at}

For example, this will work:

例如,这将起作用:

scope :foo, where(:start_date.lte=>Date.today.midnight)

#3


1  

Not sure if you'll like this method, it's not the best, but it should work.

不确定你是否喜欢这种方法,它不是最好的,但应该有效。

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  field :checked_at, :type => Time

  scope :unresolved, lambda{ |user| where(:updated_at.gt => user.checked_at) }
end

You call it with User.unresolved(my_user_object) It seems now after rereading your post that this probably won't do what you want. If this is true, then you will probably need to use MapReduce or possibly Baju's method (have not tested it)

你用User.unresolved(my_user_object)调用它现在看起来重新阅读你的帖子后,这可能不会做你想要的。如果这是真的,那么你可能需要使用MapReduce或者可能需要使用Baju的方法(尚未测试过)