查询集合中的哈希/ bson文档字段

时间:2017-06-20 21:03:58

标签: ruby-on-rails mongoid

大家好,我有一个Mongoid模型,Spec,加上mongoid-history gem就像这样:

class Spec
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::History::Trackable
  field :due_at, type: DateTime

  track_history on: [:due_at],
                :modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
                :version_field => :version,   # adds "field :version, :type => Integer" to track current version, default is :version
                :track_create   =>  false,    # track document creation, default is false
                :track_update   =>  true,     # track document updates, default is true
                :track_destroy  =>  false     # track document destruction, default is false

end

spec = Spec.first
hist = spec.history_tracks.first 
#=> { id: 123, modified: {due_at: 2017-06-12}, ... }

hist.modified.class 
#=> BSON::Document

我的问题是,我们如何通过modified字段的存在来查询due_at字段?失败的尝试看起来像这样:

spec.history_tracks.where(:'modified.due_at.exists' => true)

提前感谢!!

1 个答案:

答案 0 :(得分:0)

这应该有效:

spec.history_tracks.not.where('modified.due_at' => nil)