使用rails在mongo中的集合中嵌入的字段不会查询过滤器

时间:2015-11-11 15:29:50

标签: ruby-on-rails mongodb ruby-on-rails-4 mongoid

问题是当我尝试按集合中嵌入的字段进行过滤时,例如:

    let time = NSTimeInterval(100)

    let archivedTime = NSKeyedArchiver.archivedDataWithRootObject(time)
    let unarchivedTime = NSKeyedUnarchiver.unarchiveObjectWithData(archivedTime) as! NSTimeInterval

查询返回:

s = Select.where('expired_date' => {'$gte' => Time.zone.today}, 'options.language' => "es")

数据插入:

irb(main):019:0> s.size => 1
irb(main):021:0> s.first.options.size
selector={"$query"=>{"expired_date"=>{"$gte"=>2015-11-11 00:00:00 UTC}, "options.language"=>"es"}
=> 2

选择

Select.new(expired_date: (Date.today + 1.week), options: [{ name: "first name", language: "es"}, { name: "second name", language: "en"}]).save

方法

class Select
  include MongoidBase
  include Mongoid::Timestamps

  embeds_many :options
  field :expired_date, type: Date 

  validates :expired_date, presence: true
  validates :options, presence: true
end

在这种情况下,过滤器用于字段语言class Option include MongoidBase embedded_in :feature field :name, type: String field :language, type: String validates :name, presence: true validates :language, presence: true, inclusion: { in: ["en", "es"] } end 。这应该返回1而不是2,因为只有一个文档具有此值。

0 个答案:

没有答案