Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-1081

"w has to be a number or a string" when using w=majority in connection string

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.2
    • Affects Version/s: 2.2.1
    • Component/s: Public API
    • None
    • Environment:
      Ruby 2.3.0, Mac OS X 10.10.5, MongoDB 2.6.11, Ruby Mongo Driver 2.2.1

      Specifying a majority write concern in a connection string causes writes to fail with an operation failure of "w has to be a number or string".

      Code to reproduce:

      require 'mongo'
      
      client = Mongo::Client.new('mongodb://localhost:27017/foo?w=majority')
      client[:bar].insert_one(baz: 1)
      

      Expected output:

      A successful write.

      Actual output:

      D, [2016-01-15T16:50:39.710925 #44352] DEBUG -- : MONGODB | localhost:27017 | foo.insert | STARTED | {"insert"=>"bar", "documents"=>[{:baz=>1, :_id=>BSON::ObjectId('5699235fc78459ad40000000')}], "ordered"=>true, "writeConcern"=>{"w"=>:majority}}
      D, [2016-01-15T16:50:39.712173 #44352] DEBUG -- : MONGODB | localhost:27017 | foo.insert | FAILED | w has to be a number or a string (9) | 0.001123s
      Mongo::Error::OperationFailure: w has to be a number or a string (9)
      from /Users/mudge/.gem/ruby/2.3.0/gems/mongo-2.2.1/lib/mongo/operation/result.rb:256:in `validate!'
      

      The issue seems to be that the parsed write concern is the Symbol :majority rather than the String 'majority'. Overriding this with with fixes the issue:

      client[:bar].with(write: { w: 'majority' }).insert_one(baz: 1)
      D, [2016-01-15T16:53:08.175629 #44352] DEBUG -- : MONGODB | localhost:27017 | foo.insert | STARTED | {"insert"=>"bar", "documents"=>[{:baz=>1, :_id=>BSON::ObjectId('569923f4c78459ad40000001')}], "ordered"=>true, "writeConcern"=>{:w=>"majority"}}
      D, [2016-01-15T16:53:08.342142 #44352] DEBUG -- : MONGODB | localhost:27017 | foo.insert | SUCCEEDED | 0.16629s
      => #<Mongo::Operation::Result:70352385106360 documents=[{"ok"=>1, "n"=>1}]>
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            mudge Paul Mucur
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: