我正在通过带有 Rails 3 的 Mongoid 使用 MongoDB,并在进行查询时观察到这种奇怪的行为rails console
:
> Table.where(:field => {"$exists" => true}).count
=> 3735
> Table.where(:field => {"$exists" => true}, :field => {"$ne" => ""}).count
=> 14878 # wtf???
> Table.where(:field => {"$exists" => true}, :field => "").count
=> 0 # at least it's not negative
> Table.where(:field => {"$exists" => false}).count
=> 11143
因为11143 + 3735 = 14878
,我假设这where(:field => {"$exists" => true}, :field => {"$ne" => ""})
也计算那些:field
不存在的记录(因为nil != ""
?)。但是,我相信中列出的条件#where
将与 连接and
,因此它应该只匹配那些:field
不存在空字符串 AND 的记录。