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

and() criterion does not work with operators as Ruby symbols

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.2.1
    • Component/s: Query
    • None

      The and() criterion behaves incorrectly when Ruby symbols are passed instead of strings. The repro below demonstrates this issue. This issue is fixed by converting potential Ruby symbol usages to strings in the and() code.

      Repro

      For some DB, "Employees":

      Band.create!
      Band.create!
      e_ids = Band.all.take(2).map(&:id); nil
      

      => [BSON::ObjectId('<oid_1>'), BSON::ObjectId('<oid_2>')]

      Band.where(:id => { "$in" => e_ids }).selector
      

      => {"_id"=>{"$in"=>[BSON::ObjectId('<oid_1>')]}}

      # specifying both as strings "$in" works -

      Band.where(:id => { "$in" => e_ids }).and(:id => { "$in" => [ e_ids[1] ] }).selector
      => {"_id"=>{"$in"=>[BSON::ObjectId('<oid_1>')]}, "$and"=>[{"_id"=>{"$in"=>[BSON::ObjectId('<oid_1>')]}}]}
      
      Employees.where(:id => { "$in" => e_ids }).and(:id => { "$in" => [ e_ids[1] ] }).count
      

      => 1

      # specifying one as a symbol does not work -

      Band.where(:id => { "$in" => e_ids }).and(:id => { :$in => [ e_ids[1] ] }).selector
      

      NoMethodError: undefined method `start_with?' for :$in:Symbol

      # specifying both as symbols also does not work

      Band.where(:id => {:$in => e_ids }).and(:id => { :$in => [ e_ids[1] ] }).selector
      

      NoMethodError: undefined method `start_with?' for :$in:Symbol

      # but specifying one as a symbol without the and() does work

      Band.where(:id => { :$in => e_ids }).selector
      

      => {"_id"=>{:$in=>[BSON::ObjectId('<oid_1>')]}}

      Band.where(:id => { :$in => e_ids }).count
      => 2

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            dalton.black@braze.com Dalton Black
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: