Ruby on rails 如何在Mongoid中查找具有零字段值的嵌入文档?
有没有办法在Mongoid中的嵌入文档中找到nil值 鉴于我有这些模型:Ruby on rails 如何在Mongoid中查找具有零字段值的嵌入文档?,ruby-on-rails,ruby,mongodb,orm,mongoid,Ruby On Rails,Ruby,Mongodb,Orm,Mongoid,有没有办法在Mongoid中的嵌入文档中找到nil值 鉴于我有这些模型: class Record include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document embedded_in :record field :special_id, type: String end 我可以找到给定位置模型的特定特殊\u id的记录 Record.whe
class Record
include Mongoid::Document
embeds_many :locations
end
class Location
include Mongoid::Document
embedded_in :record
field :special_id, type: String
end
我可以找到给定位置模型的特定特殊\u id
的记录
Record.where('locations.special_id' => '123')
但是,如果我想在位置中获取所有具有nilspecial_id
的记录,那么这可以工作,但会返回所有记录
Record.where('locations.special_id.eq' => nil)
此函数返回0个结果:
Record.where('locations.special_id.exists' => false)
谢谢
嵌入了很多
只是在MongoDB中设置了一个哈希数组,所以通常的点表示法应该可以工作:
Record.where('locations.special_id' => nil)
# ------------^^^^^^^^^^^^^^^^^^^^ just the usual JavaScript-style path
.eq
和.exists
符号通常用作符号的缩写形式。例如:
where(:'locations.special_id'.ne => nil)
将是以下内容的简写:
where('locations.special_id' => { :$ne => nil })
如果您试图在字段名字符串中嵌入运算符,那么MongoDB会认为您试图在路径中使用另一个组件。如果您没有嵌入文档,可能是@juanpstas。如果
special\u id
是record
模型的一个字段,那就行了。record.where('locations.special\u id'=>nil)
?@muistooshort将其作为一个答案,我会接受它。谢谢