I made this script :
#!/usr/bin/env ruby require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'mongoid', '7.2' end require 'mongoid' Mongoid.configure { |c| c.clients.default = { hosts: ['mongo:27017'], database: 'test' } } puts Mongoid::VERSION puts Mongo::VERSION Mongoid.logger.level = 4 Mongo::Logger.logger.level = 4 class Article include Mongoid::Document field :body, type: String end pp Article.for_js('this.body == "test"').count
When I run it, it produces this error :
/usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/result.rb:364:in `raise_operation_failure': [2:BadValue]: $where is not allowed in this context (on mongo:27017, modern retry, attempt 1) (Mongo::Error::OperationFailure)
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/result.rb:330:in `validate!'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:36:in `block (3 levels) in validate_result'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:108:in `add_server_diagnostics'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:35:in `block (2 levels) in validate_result'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:54:in `add_error_labels'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:34:in `block in validate_result'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:94:in `unpin_maybe'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/response_handling.rb:33:in `validate_result'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/executable.rb:81:in `block in execute'
from <internal:kernel>:90:in `tap'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/executable.rb:80:in `execute'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/operation/shared/op_msg_executable.rb:49:in `execute_with_connection'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/aggregation.rb:188:in `block in send_initial_query'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/server/connection_pool.rb:705:in `with_connection'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/server.rb:494:in `with_connection'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/aggregation.rb:184:in `send_initial_query'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/iterable.rb:127:in `block in select_cursor'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/retryable/read_worker.rb:68:in `block in read_with_retry_cursor'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/retryable/read_worker.rb:193:in `modern_read_with_retry'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/retryable/read_worker.rb:117:in `read_with_retry'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/retryable/read_worker.rb:67:in `read_with_retry_cursor'
from /usr/local/lib/ruby/3.1.0/forwardable.rb:238:in `read_with_retry_cursor'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/iterable.rb:126:in `select_cursor'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/iterable.rb:60:in `each'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/readable.rb:233:in `first'
from /usr/local/bundle/gems/mongo-2.19.1/lib/mongo/collection/view/readable.rb:233:in `count_documents'
from /usr/local/bundle/gems/mongoid-7.2.0/lib/mongoid/contextual/mongo.rb:74:in `block in count'
from /usr/local/bundle/gems/mongoid-7.2.0/lib/mongoid/contextual/mongo.rb:555:in `try_cache'
from /usr/local/bundle/gems/mongoid-7.2.0/lib/mongoid/contextual/mongo.rb:74:in `count'
from /usr/local/lib/ruby/3.1.0/forwardable.rb:238:in `count'
from ./tmp/mongoid.rb:25:in `<main>'
The issue happens with Mongoid 7.2 and later, but not with Mongoid 7.1. The query without count succeed. Article.for_js('this.body == "test"').to_a.count work as expected.