Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5097

any_of with multiple conditions and :exists in the first one on a Time field attempts to evolve true|false to Time

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 7.3.0, 7.1.8, 7.2.3
    • Component/s: Query

      Given a model with a Time field defined:

      
      class MyModel
        include Mongoid::Document 
       field :a, type: Time
      end
       

      When using any_of with multiple arguments, which Mongoid attempts to combine, and when using $exists as a symbol operator on the time field with a boolean value, Mongoid attempts to evolve the boolean value to a time which fails:

      pp MyModel.any_of({:a.exists => true}, a: nil)
      
      
      Traceback (most recent call last):
      	14: from test.rb:34:in `<main>'
      	13: from /home/w/.rbenv/versions/2.7.2/lib/ruby/2.7.0/forwardable.rb:235:in `any_of'
      	12: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selectable.rb:714:in `any_of'
      	11: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selectable.rb:714:in `tap'
      	10: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selectable.rb:718:in `block in any_of'
      	 9: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:59:in `store'
      	 8: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:101:in `evolve_multi'
      	 7: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:101:in `map'
      	 6: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:102:in `block in evolve_multi'
      	 5: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:102:in `map'
      	 4: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:102:in `each'
      	 3: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:119:in `block (2 levels) in evolve_multi'
      	 2: from /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/selector.rb:154:in `evolve'
      	 1: from /home/w/.rbenv/versions/2.7.2/lib/ruby/2.7.0/forwardable.rb:235:in `evolve'
      /home/w/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mongoid-7.3.0/lib/mongoid/criteria/queryable/extensions/time.rb:52:in `evolve': undefined method `__evolve_time__' for true:TrueClass (NoMethodError)
      Did you mean?  __evolve_object_id__
      

      Note that `or` works when used instead of `any_of`:

      (byebug) MyModel.or({:a.exists => true},{a:nil})
      #<Mongoid::Criteria
        selector: {"$or"=>[{"a"=>{"$exists"=>true}}, {"a"=>nil}]}
        options:  {}
        class:    MyModel
        embedded: false>
      

      Note that the various symbol operators have special value conversion logic defined in Criteria::Queryable::Selectable. For example, for $exists:

              key :exists, :override, "$exists" do |value|
                Mongoid::Boolean.evolve(value)
              end
      

      When Mongoid is combining conditions with operators, it needs to follow the various rules defined in that file for the various operators that are supported (others are $type and the geo ones).

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            cesar@flashvote.com Cesar Izurieta
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: