首页 > 解决方案 > Mongo::Error::OperationFailure: $not need a regex or a document in rails mongoid

问题描述

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

配置中有什么导致这种情况的吗?

标签: ruby-on-railsmongoid

解决方案


已发布的文档表明它适用于哪个版本(左上角)。当前的 Mongoid 版本是 7.2,因此https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#logical-operations表明它适用于 7.2。您可以从下拉列表中选择您正在使用的版本。

not您所询问的行为正在按照 Mongoid 7.1 的当前文档中描述的方式工作。正如您所发现的,以前版本的 Mongoid 不像当前描述的那样工作。


推荐阅读