问题描述
Rails 应用有 Moingoid 7.0.5
class Example
include Mongoid::Document
field :name,type: String
end
当我查询 Example.not(name: "xyz").count
时,我收到此错误:
Mongo::Error::OperationFailure: $not 需要正则表达式或文档 (2) (在 localhost:27017 上,现代重试,尝试 1)(在 localhost:27017 上, 现代重试,尝试 1)
Mongoid 文档确实说我们也可以以这种方式使用 not
运算符。我确实得到了 Example.where(:name.ne => "xyz").count
的预期结果。
配置中是否存在导致这种情况的原因?
解决方法
已发布的文档指明了它适用于哪个版本(左上角)。当前的 Mongoid 版本是 7.2,因此 https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#logical-operations 表明它适用于 7.2。您可以从下拉列表中选择您正在使用的版本。
您所询问的 not
行为按照 Mongoid 7.1 的当前文档中描述的方式工作。正如您所发现的,以前版本的 Mongoid 与当前描述的不一样。