Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

Mongo::Error::OperationFailure: $not needs a regex or a document in rails mongoid

Rails app has Moingoid 7.0.5

class Example
 include Mongoid::Document
 field :name, type: String
end

And when I query Example.not(name: "xyz").count, I get this error:

Mongo::Error::OperationFailure: $not needs a regex or a document (2) (on localhost:27017, modern retry, attempt 1) (on localhost:27017, modern retry, attempt 1)

Mongoid Documentation does say that we can use not operator this way as well. I do get expected results for Example.where(:name.ne => "xyz").count.

Is there something in configuration that leads to this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The published documentation indicates which version it applies to (upper left corner). Current Mongoid release is 7.2, thus https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#logical-operations shows that it applies to 7.2. You can select the version you are using from the dropdown.

The not behavior you are asking about is working the way it is described in current documentation from Mongoid 7.1. Previous versions of Mongoid, as you found out, don't work as currently described.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...