Giter Site home page Giter Site logo

moped's Introduction

Mongoid

This is the legacy fork which is no longer maintained. The official repository is now under the MongoDB organization here.

moped's People

Contributors

ahoward avatar arthurnn avatar bernerdschaefer avatar ctrochalakis avatar davebrace avatar dblock avatar deepj avatar deiga avatar durran avatar hanshasselberg avatar huacnlee avatar jonhyman avatar kubz avatar lgustafson avatar m-pod avatar mathieuravaux avatar niels avatar nviennot avatar reidmorrison avatar rickmzp avatar rkistner avatar rodrigosaito avatar sferik avatar sporkmonger avatar thijsc avatar timoschilling avatar trumant avatar wandenberg avatar winslett avatar zarqman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moped's Issues

Errno::ECONNRESET: Connection reset by peer on Beta1

I'm getting an error "Errno::ECONNRESET: Connection reset by peer" when I believe that the mongodb connection hasn't been touched in a while. Here's the relevant part of my backtrace:

[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/connection.rb:80
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/connection.rb:80
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/connection.rb:66
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/connection.rb:65
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/connection.rb:65
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:299
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:141
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:297
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:312
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:296
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:285
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:26
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/node.rb:90
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cluster.rb:43
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cluster.rb:56
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cluster.rb:56
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cluster.rb:75
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cluster.rb:112
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/session/context.rb:92
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cursor.rb:47
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/cursor.rb:32
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/query.rb:121
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/query.rb:121
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/query.rb:120
[GEM_ROOT]/gems/moped-1.0.0.beta/lib/moped/query.rb:120
/home/deploy/.bundler/ruby/1.9.1/mongoid-70486fa67f98/lib/mongoid/contextual/mongo.rb:130
/home/deploy/.bundler/ruby/1.9.1/mongoid-70486fa67f98/lib/mongoid/contextual/mongo.rb:520
/home/deploy/.bundler/ruby/1.9.1/mongoid-70486fa67f98/lib/mongoid/contextual/mongo.rb:120
/home/deploy/.bundler/ruby/1.9.1/mongoid-70486fa67f98/lib/mongoid/contextual.rb:18
[PROJECT_ROOT]/app/views/debug/index.html.erb:13

I think the problem is that moped isn't discovering that the connection was disconnected from going idle. Would it be a good idea to catch this error at the top level or in the library and try to re-execute the request?

findAndModify changes on 2.2.0-rc0

Running specs against a mongodb 2.2.0-rc0 one spec fails:

  1) Moped::Query with a local connection#modify when the selector matches when providing options when providing upsert: true when not providing new: true returns an empty hash
     Failure/Error: result.should be_empty
     NoMethodError:
       undefined method `empty?' for nil:NilClass

findAndModify now returns null instead of and empty array:

http://docs.mongodb.org/manual/release-notes/2.2/#findandmodify-returns-null

I can fix this and send a pull request but my question is: should moped returns nil to keep compatibility with mongodb api or should moped keep returning an empty array an keep compatibility with older versions?

BSON::ObjectId instances are not `legal?`

Moped::BSON::ObjectId.legal?(Moped::BSON::ObjectId.new) # => false

This was unexpected.

You need to pass the method a string for it to work.

Moped::BSON::ObjectId.legal?(Moped::BSON::ObjectId.new.to_s) # => true

The behavior is a change from the mongo ruby gem. Not sure if it was deliberate.

Compatibility with 10gen driver

With the current master branch of Moped (tested a8a407a), I get an error when using it together with the 10gen driver.

This error was not present in moped 1.0.0.rc.

>> require 'mongo'
=> true
>> require 'moped'
NameError: uninitialized constant BSON::Extensions
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson/extensions.rb:57:in `<class:Object>'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson/extensions.rb:56:in `<module:Extensions>'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson/extensions.rb:17:in `<module:BSON>'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson/extensions.rb:16:in `<module:Moped>'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson/extensions.rb:15:in `<top (required)>'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson.rb:1:in `require'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped/bson.rb:1:in `<top (required)>'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped.rb:5:in `require'
    from .../ruby/1.9.1/moped-a8a407a3a468/lib/moped.rb:5:in `<top (required)>'

Driver broken with latest

/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:74:in block in command' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:522:in[]'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:522:in block (3 levels) in flush' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:521:inmap'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:521:in block (2 levels) in flush' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:113:inensure_connected'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:517:in block in flush' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:532:inlogging'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:516:in flush' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:505:inprocess'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:70:in command' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/node.rb:356:inrefresh'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/cluster.rb:101:in block in refresh' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/cluster.rb:114:ineach'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/cluster.rb:114:in refresh' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/cluster.rb:67:innodes'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/cluster.rb:136:in with_primary' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/session/context.rb:108:inwith_node'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/session/context.rb:43:in query' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/moped-1.1.1/lib/moped/query.rb:108:infirst'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/bundler/gems/mongoid-21d194b91d33/lib/mongoid/contextual/mongo.rb:199:in first' /Users/peter/.rvm/gems/ruby-1.9.2-p320/bundler/gems/mongoid-21d194b91d33/lib/mongoid/contextual.rb:18:infirst'
/Users/peter/Development/nulayer/pressly-api/lib/pressly-api/middleware/property_scoper.rb:40:in call' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/puma-1.4.0/lib/puma/server.rb:420:inhandle_request'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/puma-1.4.0/lib/puma/server.rb:288:in process_client' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/puma-1.4.0/lib/puma/server.rb:197:inblock in run'
/Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/puma-1.4.0/lib/puma/thread_pool.rb:92:in call' /Users/peter/.rvm/gems/ruby-1.9.2-p320/gems/puma-1.4.0/lib/puma/thread_pool.rb:92:inblock in spawn_thread'

.. I've tried this for really even the simplistic queries and the thing happens.

Moped errors out when DNS does not resolve

Hi,

I am using Mongo with Moped on a replica set and occasionally I see this error. Moped fails when its unable to resolve the address of the application.

Could you please let me know the following details which could be helpful for debugging such situations ?

Socket.getaddrinfo is used at moped/node.rb.

How often is this method called on every request to Rails / Mongo vs Once the app starts ? ( since there is no pooling of resources now in Mongoid 3)

From random testing, I can see that there is few hundred milli seconds spent in resolving the hostname to an IP address when trying from command line.

Is it not possible to determine the node status as down in case DNS is not resolved ? ( Fail silently if its the secondary or not the primary)

Below is the stack trace from the application logs . JRuby / Moped - 1.2.0

org/jruby/ext/socket/RubySocket.java:711:in `getaddrinfo'
gems/gems/moped-1.2.0/lib/moped/node.rb:214:in `initialize'
gems/gems/moped-1.2.0/lib/moped/node.rb:378:in `refresh'
org/jruby/RubyArray.java:2339:in `collect'
gems/gems/moped-1.2.0/lib/moped/node.rb:378:in `refresh'
gems/gems/moped-1.2.0/lib/moped/cluster.rb:112:in `refresh'
org/jruby/RubyArray.java:1615:in `each'
gems/gems/moped-1.2.0/lib/moped/cluster.rb:125:in `refresh'
gems/gems/moped-1.2.0/lib/moped/cluster.rb:78:in `nodes'
gems/gems/moped-1.2.0/lib/moped/cluster.rb:187:in `with_secondary'
gems/gems/moped-1.2.0/lib/moped/session/context.rb:104:in `with_node'
gems/gems/moped-1.2.0/lib/moped/session/context.rb:43:in `query'
gems/gems/moped-1.2.0/lib/moped/query.rb:109:in `first'
gems/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:202:in `first'
org/jruby/RubyBasicObject.java:1698:in `__send__'
gems/gems/mongoid-3.0.4/lib/mongoid/contextual.rb:18:in `first'
gems/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongoid.rb:32:in `get'
gems/gems/devise-2.0.4/lib/devise/models/authenticatable.rb:138:in `serialize_from_session'
gems/gems/devise-2.0.4/lib/devise/rails/warden_compat.rb:27:in `deserialize'
gems/gems/warden-1.1.1/lib/warden/session_serializer.rb:31:in `fetch'
gems/gems/warden-1.1.1/lib/warden/proxy.rb:196:in `user'
gems/gems/warden-1.1.1/lib/warden/proxy.rb:293:in `_perform_authentication'
gems/gems/warden-1.1.1/lib/warden/proxy.rb:90:in `authenticate'
gems/gems/devise-2.0.4/lib/devise/controllers/helpers.rb:XX:in `current_user'
gems/gems/devise-2.0.4/lib/devise/controllers/helpers.rb:XX:in `user_signed_in?'
app/controllers/application_controller.rb:XX:in `auth_user!'
gems/gems/activesupport-3.2.5/lib/active_support/callbacks.rb:432:in 

Moped 1.2.7 takes a long time to report failure

Moped 1.2.7 takes a long time to report a connection failure similar to this:

Moped::Errors::ConnectionFailure: Could not connect to a primary node for replica set <Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>

This is causing tests to take excessive time to report such failure. In particular, a test suite that used to take about 2.5 seconds to run is now taking 20 minutes.

This behavior is new in 1.2.7, caused by the new logic for gracefully handling crashes. (2d0ea12)

The solution seems to be to set the value of max_retries to 0. It seems logical to set this value whenever running in a test environment. So something along these lines might be useful:

if defined?(Rails) && Rails.env == 'test'
  max_retries = 0
end

Load Balancing (Reads) in a Replica Set

Does Moped do any sort of load balancing across nodes in a replica set? That should happen for reads, right?

I took a look at Moped::Session.new -- didn't see any sort of "rotation" through the hosts for reading. Does it happen somewhere else? Would it make sense to do this?

I'm also ok with handling this at the app level. What is the recommended way to go about it in Moped and/or Mongoid?

`Query#explain` always returns 1 for n, nScanned, and nScannedObjects

I always get 1 for n, nScanned, and nScannedObjects in my calls to explain. This differs from what I expect and what I get from the mongo console. Example:

Moped / Mongoid:

User.collection.find({created_at: {:$exists => false}}).explain

Result:
{"cursor"=>"BasicCursor", "nscanned"=>1, "nscannedObjects"=>1, "n"=>1, "millis"=>0, "nYields"=>0, "nChunkSkips"=>0, "isMultiKey"=>false, "indexOnly"=>false, "indexBounds"=>{}, "allPlans"=>[{"cursor"=>"BasicCursor", "indexBounds"=>{}}], "oldPlan"=>{"cursor"=>"BasicCursor", "indexBounds"=>{}}}

Log output from that query + explain:
MOPED: 127.0.0.1:27017 QUERY database=exfero_development collection=users selector={"$query"=>{:created_at=>{:$exists=>false}}, "$orderby"=>{}, "$explain"=>true} flags=[:slave_ok] limit=-1 skip=0 fields=nil (0.4332ms)

Mongo console:

db.users.find({created_at: {$exists: false}}).explain()

Result:
{ "cursor" : "BasicCursor", "nscanned" : 10421, "nscannedObjects" : 10421, "n" : 10420, "millis" : 12, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { } }

Here I've done a query where I knew I didn't have an index, but it doesn't seem to make a difference. It's always 1 for all queries I've tried, with or without an index.

Unauthenticated Cursor Continues to issue Queries

From mongoid/mongoid#2355

In authenticated environments, we are seeing random cursors which do not authenticate. Other cursors from the same source are authenticating properly. In the MongoDB logs, these unauthenticated cursors are not even attempting to authenticate. I've seen this 3 times over the past week, but appears to be unpredictable.

In the most recent scenario, an unauthenticated cursor occurred after a step down of the Replica Set. The cursor was connected to the server that stepped down, was previously Primary, and now a Secondary.

The following was the MongoDB log:

Thu Sep 6 16:10:55 [initandlisten] connection accepted from 10.82.11.157:39608 #912
Thu Sep 6 16:10:55 [conn912] assertion 10057 unauthorized db:myDatabase lock type:-1 client:10.82.11.156 ns:myDatabase.users query:{ $query: { _id: ObjectId('4f6b0a7dcf2bee4c65000001') }, $orderby: { _id: 1 } }

The cursor lasted for 5 minutes and tried 33 queries on the "users" collection.

This issue cannot be predictably reproduced, but I wanted to put it on the issues as I've seen it a few times with a few different clients over the past week. It is happening with v3.0.5.

Moped::Node #ensure_connected when node is running fails on Ruby 1.9.2

Moped::Node
  #ensure_connected
    when node is running
      processes the block (FAILED - 1)
 1) Moped::Node#ensure_connected when node is running processes the block
     Failure/Error: node.command("admin", ping: 1)
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./lib/moped/node.rb:74:in `block in command'
     # ./lib/moped/node.rb:522:in `[]'
     # ./lib/moped/node.rb:522:in `block (3 levels) in flush'
     # ./lib/moped/node.rb:521:in `map'
     # ./lib/moped/node.rb:521:in `block (2 levels) in flush'
     # ./lib/moped/node.rb:107:in `ensure_connected'
     # ./lib/moped/node.rb:517:in `block in flush'
     # ./lib/moped/node.rb:532:in `logging'
     # (eval):5:in `logging'
     # ./lib/moped/node.rb:516:in `flush'
     # ./lib/moped/node.rb:505:in `process'
     # ./lib/moped/node.rb:70:in `command'
     # ./spec/moped/node_spec.rb:16:in `block (5 levels) in <top (required)>'
     # ./lib/moped/node.rb:113:in `ensure_connected'
     # ./spec/moped/node_spec.rb:15:in `block (4 levels) in <top (required)>'

This causes things like purge! to fail under Ruby 1.9.2. Works with 1.9.2.

Also need to add ruby 1.9.2 to travis.

undefined method `__bson_dump__'

I get this error when I try to push to an embeds_many item. My code example that triggers this error is:

user.access_grants << access_grant
Looking at what user.access_grants.class returns Array

Backtrace:

Exception: NoMethodError: undefined method `__bson_dump__' for #<ClientApplication:0x007f3c1d6c0880>
--
0: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/mongoid-3.0.1/lib/mongoid/attributes.rb:225:in `method_missing'
1: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
2: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `each'
3: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
4: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
5: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `each'
6: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
7: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
8: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `each'
9: /home/sean/.rvm/gems/ruby-1.9.3-p194@hub/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'

List methods available to user.access_grants:

ls user.access_grants
Mongoid::Relations::Proxy#methods: base  base=  bind_one  collection_name  extend_proxy  foreign_key  init  inverse_foreign_key  klass  loaded  loaded=  metadata  metadata=  substitutable  target  target=  unbind_one  with
Mongoid::Relations::Many#methods: avg  blank?  create  create!  exists?  find_or_create_by  find_or_initialize_by  length  max  min  nil?  respond_to?  scoped  serializable_hash  size  sum
Mongoid::Relations::Embedded::Batchable#methods: batch_clear  batch_insert  batch_remove  batch_replace
Mongoid::Relations::Embedded::Many#methods: <<  as_document  build  clear  concat  count  delete  delete_all  destroy_all  find  in_memory  new  pop  push  substitute  unscoped
instance variables: @_unscoped  @base  @metadata

Relevant gems I am using:

  • bson 1.5.2
  • bson_ext 1.5.2
  • mongoid 3.0.1
  • activemodel 3.2.1
  • moped 1.1.1
  • origin 1.0.4
  • mongo 1.5.2

Please tell me if this is unrelated to moped.

Encoding::UndefinedConversionError: "\x97" from ASCII-8BIT to UTF-8

I'm having the following encoding issue which I'm not quite sure how to fix. It appears to be related to moped :-)

My code:

    class Cache
      include Mongoid::Document
    end
    __ENCODING__
    value = {
    :expected_result => false,
         :conclusive => false,
              :score => 0,
         :step_score => {
        "5026c3b904e38d265b000008" => 100,
        "5026c3b904e38d265b000009" => -100
    },
     :required_score => 50,
       :any_failures => true,
      :any_successes => true,
        :any_yes_but => false,
      :yes_but_steps => {},
       :any_problems => false,
      :problem_steps => {}
}
    tmp = Marshal.dump(value)
    ap tmp
    Cache.find_or_create_by(:key => 'testing').set(:value, Marshal.dump(tmp))

Output:

1.9.3p194 :001 > __ENCODING__
 => #<Encoding:UTF-8> 
1.9.3p194 :002 >   
1.9.3p194 :003 >       value = {
1.9.3p194 :004 >         :expected_result => false,
1.9.3p194 :005 >              :conclusive => false,
1.9.3p194 :006 >                   :score => 0,
1.9.3p194 :007 >              :step_score => {
1.9.3p194 :008 >               "5026c3b904e38d265b000008" => 100,
1.9.3p194 :009 >               "5026c3b904e38d265b000009" => -100
1.9.3p194 :010?>         },
1.9.3p194 :011 >          :required_score => 50,
1.9.3p194 :012 >            :any_failures => true,
1.9.3p194 :013 >           :any_successes => true,
1.9.3p194 :014 >             :any_yes_but => false,
1.9.3p194 :015 >           :yes_but_steps => {},
1.9.3p194 :016 >            :any_problems => false,
1.9.3p194 :017 >           :problem_steps => {}
1.9.3p194 :018?>   }
 => {:expected_result=>false, :conclusive=>false, :score=>0, :step_score=>{"5026c3b904e38d265b000008"=>100, "5026c3b904e38d265b000009"=>-100}, :required_score=>50, :any_failures=>true, :any_successes=>true, :any_yes_but=>false, :yes_but_steps=>{}, :any_problems=>false, :problem_steps=>{}} 
1.9.3p194 :019 >     tmp = Marshal.dump(value)
 => "\x04\b{\x10:\x14expected_resultF:\x0FconclusiveF:\nscorei\x00:\x0Fstep_score{\aI\"\x1D5026c3b904e38d265b000008\x06:\x06ETiiI\"\x1D5026c3b904e38d265b000009\x06;\tTi\x97:\x13required_scorei7:\x11any_failuresT:\x12any_successesT:\x10any_yes_butF:\x12yes_but_steps{\x00:\x11any_problemsF:\x12problem_steps{\x00" 
1.9.3p194 :020 > ap tmp
"\x04\b{\x10:\x14expected_resultF:\x0FconclusiveF:\nscorei\x00:\x0Fstep_score{\aI\"\x1D5026c3b904e38d265b000008\x06:\x06ETiiI\"\x1D5026c3b904e38d265b000009\x06;\tTi\x97:\x13required_scorei7:\x11any_failuresT:\x12any_successesT:\x10any_yes_butF:\x12yes_but_steps{\x00:\x11any_problemsF:\x12problem_steps{\x00"
 => "\x04\b{\x10:\x14expected_resultF:\x0FconclusiveF:\nscorei\x00:\x0Fstep_score{\aI\"\x1D5026c3b904e38d265b000008\x06:\x06ETiiI\"\x1D5026c3b904e38d265b000009\x06;\tTi\x97:\x13required_scorei7:\x11any_failuresT:\x12any_successesT:\x10any_yes_butF:\x12yes_but_steps{\x00:\x11any_problemsF:\x12problem_steps{\x00" 
1.9.3p194 :021 >     Cache.find_or_create_by(:key => 'testing').set(:value, Marshal.dump(tmp))
Encoding::UndefinedConversionError: "\xED" from ASCII-8BIT to UTF-8
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/string.rb:33:in `encode'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/string.rb:33:in `to_utf8_binary'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/string.rb:16:in `__bson_dump__'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/hash.rb:35:in `each'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/hash.rb:35:in `each'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/bson/document.rb:11:in `serialize'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/protocol/message.rb:136:in `serialize_update'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/protocol/message.rb:289:in `serialize'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/connection.rb:137:in `block in write'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/connection.rb:135:in `each'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/connection.rb:135:in `write'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/node.rb:517:in `block (2 levels) in flush'
... 7 levels...
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/session/context.rb:109:in `block in with_node'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/cluster.rb:150:in `block in with_primary'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/node.rb:168:in `ensure_primary'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/cluster.rb:149:in `with_primary'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/session/context.rb:108:in `with_node'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/session/context.rb:69:in `update'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/query.rb:306:in `block in update'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/session.rb:305:in `with'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.0/lib/moped/query.rb:305:in `update'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.3/lib/mongoid/persistence/atomic/operation.rb:111:in `execute'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.3/lib/mongoid/persistence/atomic/sets.rb:21:in `block in persist'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.3/lib/mongoid/persistence/atomic/operation.rb:78:in `prepare'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.3/lib/mongoid/persistence/atomic/sets.rb:19:in `persist'
    from /Users/russ/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.3/lib/mongoid/persistence/atomic.rb:207:in `set'
    from (irb):21
    from /Users/russ/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'1.9.3p194 :022 > 

Moped driver should support a replica set configuration over SSL with non-standard ports

Currently when the Ruby Mongo driver is configured to use SSL in a replica set on non-standard ports, it ignores the provided ports in favor of config info it gets from the server. For example:
Mongo::ReplSetConnection.new(['host0:37017', 'host1:37017'], :ssl => true)
will use config from the server and ignore the ports provided.

Moped should use the ports provided in the config, and not necessarily those provided from the server.

Original post: https://groups.google.com/forum/?fromgroups#!topic/mongoid/MwtB70b30yc

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8

When migrating from a legacy database to mongodb using mongoid 3 moped is generating the above error message:

Connecting to {:adapter=>"mysql", :host=>"127.0.0.1", :port=>"3306", :username=>"root", :password=>"", :database=>"w2d_dev", :encoding=>"utf8"}
LV pairs -->>>>>>>     {"id"=>"Makanan", "ru"=>"Пища", "zh"=>"食品", "jp"=>"食品", "ko"=>"음식", "fr"=>"Gastronomie", "de"=>"Nahrungsmittel", "en"=>"Food"}

<Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8>
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/regexp.rb:29:in `__bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:37:in `block in __bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `__bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/array.rb:32:in `block in __bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/array.rb:31:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/array.rb:31:in `each_with_index'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/array.rb:31:in `__bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:37:in `block in __bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `__bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:37:in `block in __bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `__bson_dump__'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/bson/document.rb:11:in `serialize'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/protocol/message.rb:136:in `serialize_selector'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/protocol/message.rb:296:in `block (2 levels) in serialize'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/protocol/message.rb:295:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/protocol/message.rb:295:in `block in serialize'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/protocol/message.rb:292:in `tap'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/protocol/message.rb:292:in `serialize'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/connection.rb:137:in `block in write'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/connection.rb:135:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/connection.rb:135:in `write'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:489:in `block (2 levels) in flush'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:113:in `ensure_connected'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:488:in `block in flush'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:503:in `logging'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:487:in `flush'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:476:in `process'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:70:in `command'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/session/context.rb:39:in `block in command'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/session/context.rb:97:in `block in with_node'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/cluster.rb:135:in `block in with_primary'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/node.rb:158:in `ensure_primary'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/cluster.rb:134:in `with_primary'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/session/context.rb:96:in `with_node'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/session/context.rb:38:in `command'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/database.rb:83:in `command'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/moped-1.0.0.rc/lib/moped/query.rb:36:in `count'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/contextual/mongo.rb:56:in `count'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/contextual.rb:18:in `count'
  /Users/dbk/Development/w2d/app/models/unique_in_locale_validator.rb:13:in `search_locale'
  /Users/dbk/Development/w2d/app/models/unique_in_locale_validator.rb:33:in `block in unique_in_locale'
  /Users/dbk/Development/w2d/app/models/unique_in_locale_validator.rb:32:in `each'
  /Users/dbk/Development/w2d/app/models/unique_in_locale_validator.rb:32:in `unique_in_locale'
  /Users/dbk/Development/w2d/app/models/unique_in_locale_validator.rb:3:in `validate_each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validator.rb:153:in `block in validate'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validator.rb:150:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validator.rb:150:in `validate'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:310:in `_callback_before_191'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:440:in `_run__3683343782699780073__validate__85299118211278988__callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/callbacks.rb:95:in `run_callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validations.rb:212:in `run_validations!'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:447:in `_run__3683343782699780073__validation__85299118211278988__callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/callbacks.rb:95:in `run_callbacks'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validations.rb:179:in `valid?'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/validations.rb:77:in `valid?'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activemodel-3.2.3/lib/active_model/validations.rb:187:in `invalid?'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/persistence/insertion.rb:22:in `prepare'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/persistence.rb:49:in `insert'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/persistence.rb:188:in `upsert'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/persistence.rb:75:in `save!'
  /Users/dbk/Development/w2d/db/seed/create_classification_collection.rb:38:in `block in <top (required)>'
  /Users/dbk/Development/w2d/db/seed/create_classification_collection.rb:29:in `each'
  /Users/dbk/Development/w2d/db/seed/create_classification_collection.rb:29:in `<top (required)>'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
  /Users/dbk/Development/w2d/db/seeds.rb:35:in `<top (required)>'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `block in load'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@rails32/gems/mongoid-3.0.0.rc/lib/mongoid/railties/database.rake:13:in `block (2 levels) in <top (required)>'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
  /Users/dbk/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `load'
  /Users/dbk/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `<main>'
rake aborted!
incompatible character encodings: ASCII-8BIT and UTF-8

It is crashing out on validation when searching the database by regexp. Below is the validator:

class UniqueInLocaleValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    (record.errors[attribute] << "#{record.class.to_s} is not unique in locale #{@already_in_locales} - please use another name") if !unique_in_locale(record, attribute)
  end

private
  def search_locale(record, attribute, locale, siblings)
    if !record.send("#{attribute.to_s}_translations")[locale].blank?
      pattern_locale = record.send("#{attribute.to_s}_translations")[locale]
      pattern = /\A(#{pattern_locale})\z/i
      checkrecord = record.class.all_of("#{attribute.to_s}.#{locale}" => pattern)
      checkrecord = checkrecord.and(siblings) if !siblings.empty?
      rcount = checkrecord.count

      if rcount == 0
        return false
      elsif rcount == 1 && record.persisted? && record.id == checkrecord.first.id
        return false
      else
        return true
      end
    end  
  end

  def unique_in_locale(record, attribute)
    @already_in_locales = ""
    siblings = {}
    siblings.merge!(:parent_id => record.parent_id) if record.respond_to?("parent_id")
    siblings.merge!(:level => record.level) if record.respond_to?("level")
    if record.fields[attribute.to_s].options[:localize] == true
      start_locale = I18n.locale
      record[attribute].keys.each do |locale|
        @already_in_locales = ("#{I18n.locale.to_s}" + " ") if search_locale(record, attribute,locale, siblings) 
      end 
      I18n.locale = start_locale
    end
    if @already_in_locales.blank? 
      return true
    else
      return false
    end
  end
end

replset support broken mongo-1.8

Moped can't connect to a mongo-1.8 replica set.

Consider this code:

#!/usr/bin/env ruby

require 'rubygems'
require 'moped'

seeds = ["reg3.hetzner.africa:27017", "reg1.hetzner.africa:27017", "reg2.hetzner.africa:27017"]
session = Moped::Session.new(seeds, database: 'puppet')
session.login 'mcolreg', 'Oghah0Oh'
puts session[:nodes].find(fqdn: 'reg1.hetzner.africa').first

It raises a timeout.

I've analyzed a packet capture and discovered that moped connects successfully to a peer and acquires the list of peers. However, the server sends peer hostnames without port numbers. In Node#initialize, moped calls to_i on a nil value to get port number zero, and tries to connect to TCP port zero on the primary. Since the server refuses to chat on TCP port zero, moped times out. Here's Node#initialize, for your convenience:

def initialize(address)
  @address = address

  host, port = address.split(":")
  @ip_address = ::Socket.getaddrinfo(host, nil, ::Socket::AF_INET, ::Socket::SOCK_STREAM).first[3]
  @port = port.to_i
  @resolved_address = "#{@ip_address}:#{@port}"

  @timeout = 5 
  @down_at = nil
  @refreshed_at = nil
  @primary = nil
  @secondary = nil
end

This doesn't happen on mongo-2.2.0, where moped works great.

Thanks,
Sheldon.

Crashes on ruby 1.9.2

Hey folks, don't know what's exactly the problem but it seems that it crashes on ruby 1.9.2 when used with mongoid and will_paginate

User.where(status: 'active').paginate(page: 1)

results in

NoMethodError: undefined method `[]' for nil:NilClass
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:74:in `block in command'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:521:in `[]'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:521:in `block (3 levels) in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:520:in `map'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:520:in `block (2 levels) in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:123:in `ensure_connected'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:516:in `block in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:531:in `logging'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:515:in `flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:504:in `process'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:70:in `command'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/node.rb:366:in `refresh'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/cluster.rb:112:in `block in refresh'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/cluster.rb:125:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/cluster.rb:125:in `refresh'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/cluster.rb:78:in `nodes'
... 2 levels...
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/cursor.rb:115:in `load_docs'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/cursor.rb:25:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/query.rb:77:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.1/lib/moped/query.rb:77:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:134:in `block in each'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:633:in `selecting'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:133:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual.rb:18:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/criteria.rb:662:in `entries'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/criteria.rb:662:in `method_missing'
    from (irb):6
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'irb(main):007:0> 

yeah, it says 1.9.1 but it's on heroku and it's actually this version

heroku run 'ruby -v' --app doshmosh
Running `ruby -v` attached to terminal... up, run.1
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

on ruby 1.9.3 everything works fine

GridFS support

Are there any plans to support GridFS in moped? I currently have some mongoid models that store binary assets in GridFS, they can't be ported forward to mongoid 3.x due to lack of GridFS support in moped. If support is desirable but hasn't been started yet I'm happy to implement and contribute it.

Error with Devise devise-2.1.0

when i called current_user, i got this error message "can't convert Array into String"

moped (1.0.0.rc) lib/moped/bson/object_id.rb:68:in __bson_dump__' moped (1.0.0.rc) lib/moped/bson/extensions/hash.rb:37:inblock in bson_dump'
moped (1.0.0.rc) lib/moped/bson/extensions/hash.rb:36:in each' moped (1.0.0.rc) lib/moped/bson/extensions/hash.rb:36:inbson_dump'
moped (1.0.0.rc) lib/moped/bson/document.rb:11:in serialize' moped (1.0.0.rc) lib/moped/protocol/message.rb:136:inserialize_selector'
moped (1.0.0.rc) lib/moped/protocol/message.rb:296:in block (2 levels) in serialize' moped (1.0.0.rc) lib/moped/protocol/message.rb:295:ineach'
moped (1.0.0.rc) lib/moped/protocol/message.rb:295:in block in serialize' moped (1.0.0.rc) lib/moped/protocol/message.rb:292:intap'
moped (1.0.0.rc) lib/moped/protocol/message.rb:292:in serialize' moped (1.0.0.rc) lib/moped/connection.rb:137:inblock in write'
moped (1.0.0.rc) lib/moped/connection.rb:135:in each' moped (1.0.0.rc) lib/moped/connection.rb:135:inwrite'
moped (1.0.0.rc) lib/moped/node.rb:489:in block (2 levels) in flush' moped (1.0.0.rc) lib/moped/node.rb:113:inensure_connected'
moped (1.0.0.rc) lib/moped/node.rb:488:in block in flush' moped (1.0.0.rc) lib/moped/node.rb:503:inlogging'
moped (1.0.0.rc) lib/moped/node.rb:487:in flush' moped (1.0.0.rc) lib/moped/node.rb:476:inprocess'
moped (1.0.0.rc) lib/moped/node.rb:309:in query' moped (1.0.0.rc) lib/moped/session/context.rb:32:inblock in query'
moped (1.0.0.rc) lib/moped/session/context.rb:93:in block in with_node' moped (1.0.0.rc) lib/moped/cluster.rb:176:inwith_secondary'
moped (1.0.0.rc) lib/moped/session/context.rb:92:in with_node' moped (1.0.0.rc) lib/moped/session/context.rb:31:inquery'
moped (1.0.0.rc) lib/moped/query.rb:108:in first' gems/mongoid/lib/mongoid/contextual/mongo.rb:196:infirst'
gems/mongoid/lib/mongoid/contextual.rb:18:in first' orm_adapter (0.0.7) lib/orm_adapter/adapters/mongoid.rb:32:inget'
devise (2.1.0) lib/devise/models/authenticatable.rb:146:in serialize_from_session' devise (2.1.0) lib/devise/rails/warden_compat.rb:29:indeserialize'
warden (1.1.1) lib/warden/session_serializer.rb:31:in fetch' warden (1.1.1) lib/warden/proxy.rb:196:inuser'
warden (1.1.1) lib/warden/proxy.rb:293:in _perform_authentication' warden (1.1.1) lib/warden/proxy.rb:90:inauthenticate'
devise (2.1.0) lib/devise/controllers/helpers.rb:56:in current_user' devise (2.1.0) lib/devise/controllers/helpers.rb:52:inuser_signed_in?'
lib/controllers/components.rb:56:in `check_session' # this is : if user_signed_in?

add support for gt/lt on ObjectId's

Currently, doing a .gt(_id: some_object_id) in mongoid throws:

NoMethodError: undefined method `>' for "500c49d9202cb1f644000001":Moped::BSON::ObjectId

Since ObjectId's have a time component, I think it makes sense to be able to filter by id range (where appropriate)

Fix Collection Names Regex

From mongoid/mongoid#2307

I spent a couple of hours to find out why Mongoid.default_session.collection_names returns empty array. I started searching in implementation and found this line:

namespaces = Collection.new(self, "system.namespaces").find(name: { "$not" => /system|$/ })

My database name was 'sth_system_development' so i changed it to 'sth_development' and now everything works fine.
I think that this regular expression should be changed to "starts with" or something else.

Connecting with a URI causes error

When I set up my configuration with a URI, like for MongoHQ on Heroku, I get the following error:

The operation: # Moped::Protocol::Query
@Length=144
@request_id=5
@response_to=0
@op_code=2004
@flags=[:slave_ok]
@full_collection_name="..."
@Skip=0
@limit=0
@selector={"$query"=>{...}}
@fields=nil>
failed with error 13075: "db name can't be empty"

NoMethodError: undefined method `each' for "[]":String

This is happening when I try to start my rails 3.2 server with the mongoid 3.0.0.rc gem on ruby 1.9.2p180 OS X Lion

I can reproduce it in IRB just the same though.
Looks like it's failing just trying to run the class level delegate method calls

$ irb
ruby-1.9.2-p180-patched :001 > require 'moped'
NoMethodError: undefined method `each' for "[]":String
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/1.9.1/forwardable.rb:153:in `block in instance_delegate'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/1.9.1/forwardable.rb:151:in `each'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/1.9.1/forwardable.rb:151:in `instance_delegate'
    from /Users/kvirani/.rvm/gems/ruby-1.9.2-p180-patched/gems/moped-1.0.0.rc/lib/moped/session.rb:43:in `<class:Session>'
    from /Users/kvirani/.rvm/gems/ruby-1.9.2-p180-patched/gems/moped-1.0.0.rc/lib/moped/session.rb:30:in `<module:Moped>'
    from /Users/kvirani/.rvm/gems/ruby-1.9.2-p180-patched/gems/moped-1.0.0.rc/lib/moped/session.rb:1:in `<top (required)>'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/kvirani/.rvm/gems/ruby-1.9.2-p180-patched/gems/moped-1.0.0.rc/lib/moped.rb:18:in `<top (required)>'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from (irb):1
    from /Users/kvirani/.rvm/rubies/ruby-1.9.2-p180-patched/bin/irb:16:in `<main>'

Installed mongo db via homebrew 0.9
Can start, connect to and run queries on mongo db just fine using the vanilla CLI

Gem list:
mongo (1.6.2)
mongoid (3.0.0.rc, 2.4.10)
moped (1.0.0.rc)

Count Command on Sharded Collection Inaccuracy

I wasn't sure where to put this issue, but it seems more like a Moped problem than a Mongoid problem.

I have two related models

class MyUpload
  include Mongoid::Document

  has_many :my_documents
end

class MyDocument
  include Mongoid::Document

  shard_key :my_upload_id, :_id

  field :_id, type: Moped::BSON::ObjectId, default: -> { Moped::BSON::ObjectId(SecureRandom.hex(12)) }
  belongs_to :my_upload
end

The my_documents collection is setup with three shards.

When I run a count of my_documents on an instance of MyUpload which has 328 documents the count method returns 0.

u = MyUpload.last
u.my_documents.count # returns 0 even though there are 328 in the collection

The corresponding mongo console query returns 328 as expected

db.my_documents.find({ "my_upload_id": ObjectId(...) }).count() // returns 328 as expected

I noticed in the Moped log, the previous Mongoid count command generates the following line

MOPED: 10.1.16.201:27017 COMMAND      database=my_database command={:count=>:my_documents, 
:query=>{"my_upload_id"=>"500d71bdd0821da854000004"}} (4.0605ms)

I noticed the value for :count is the collection name as a Symbol rather than a String
When I run

session = Moped::Session.new(["localhost:27017"])
session.use :my_database
session.login <username>, <password>
# collection name as a Symbol
session[:my_documents].find({ "my_upload_id" => Moped::BSON::ObjectId(...) }).count
# returns 0

However, if I run the same command with the collection name as a String it returns the expected result of 328.

# collection name as a String
session["my_documents"].find({ ... }).count
# returns 328 as expected

So it appears there is an issue with the collection name being a symbol vs. string when querying a sharded collection. I have confirmed that is does not happen in a non-sharded collection. Still not sure if this is an actually bug or something with my specific implementation. Any help or advice would be appreciated.

My mongoid.yml looks something like this:

sessions:
  default:
    database: my_database
    hosts:
      - <mongos_hostname>:27017
    username: <username>
    password: <password>
    options:
      consistency: :strong
      safe: true
  options:
    use_utc: true

tl;dr I've also turned on profiling level 2 on each of the shards and can see the count commands in question logged. Not sure if this is helpful or not.

{ "ts" : ISODate(...), "op" : "command", "ns" : "my_database.$cmd", "comm
and" : { "count" : "my_documents", "query" : { "my_upload_id" : ObjectId(...) } }, 
"ntoreturn" : 1, "responseLength" : 48, "millis" : 0, "client" : "127.0.1.1", "user" : "" }

Intermittent Moped::Errors::OperationFailure failed with error nil

We've just upgraded to Mongoid 3 (https://github.com/crowdtap/mongoid/tree/3.0.0-stable) and Moped (https://github.com/nviennot/moped/) and we're seeing the following errors pop up intermittently. It might be prudent to reconnect at this point and retry. I'd be happy to put something together but woud like some feedback before doing so.

Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command @length=58 @request_id=19 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="admin.$cmd" @skip=0 @limit=-1 @selector={:ismaster=>1} @fields=nil> failed with error nil

[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:75:in `block in command'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:542:in `[]'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:542:in `block (3 levels) in flush'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:541:in `map'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:541:in `block (2 levels) in flush'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:124:in `ensure_connected'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:537:in `block in flush'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:552:in `logging'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:536:in `flush'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:525:in `process'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:71:in `command'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/node.rb:386:in `refresh'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/cluster.rb:112:in `block in refresh'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/cluster.rb:125:in `each'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/cluster.rb:125:in `refresh'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/cluster.rb:78:in `nodes'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/cluster.rb:147:in `with_primary'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/session/context.rb:108:in `with_node'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/session/context.rb:43:in `query'
[GEM_ROOT]/bundler/gems/moped-0d8aa283fbe4/lib/moped/query.rb:109:in `first'
[GEM_ROOT]/bundler/gems/mongoid-3a30f32025d6/lib/mongoid/contextual/mongo.rb:201:in `first'
[GEM_ROOT]/bundler/gems/mongoid-3a30f32025d6/lib/mongoid/contextual.rb:18:in `first'

Add a Session#list_databases method

Right now this is possible by doing a

Mongoid.session(:default).with(database: :admin).command({listDatabases:1})

but it should be hidden behind a Moped method.
We maybe could submit a pull request sometime next week, right now schedule is packed ;)

Best regards!
Martin

undefined method `__bson_dump__'

I'm using derekharmel/sunspot_mongo for search and have got this error. Let me know if you need more info.

NoMethodError: undefined method `__bson_dump__' for BSON::ObjectId('4fffa1b97151e20700000b40'):BSON::ObjectId
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/array.rb:33:in `__bson_dump__'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/bson/document.rb:11:in `serialize'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/protocol/message.rb:136:in `serialize_selector'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/protocol/message.rb:289:in `serialize'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/connection.rb:137:in `block in write'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/connection.rb:135:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/connection.rb:135:in `write'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:518:in `block (2 levels) in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:113:in `ensure_connected'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:517:in `block in flush'
... 15 levels...
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.0/lib/mongoid/contextual.rb:18:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/abstract_search.rb:228:in `block in populate_hits'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/abstract_search.rb:224:in `each_pair'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/abstract_search.rb:224:in `populate_hits'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/hit.rb:90:in `result'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/abstract_search.rb:275:in `block in verified_hits'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/paginated_collection.rb:50:in `select'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/paginated_collection.rb:50:in `method_missing'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/abstract_search.rb:275:in `verified_hits'
    from /app/vendor/bundle/ruby/1.9.1/gems/sunspot-1.3.3/lib/sunspot/search/abstract_search.rb:59:in `results'
    from (irb):13
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'

versions

ruby 1.9.3p125

    mongoid (3.0.1)
      activemodel (~> 3.1)
      moped (~> 1.1.1)
      origin (~> 1.0.3)
      tzinfo (~> 0.3.22)
    mongoid-rspec (1.4.6)
      mongoid (>= 3.0.0.rc)
      rake
      rspec (>= 2.9)
    moped (1.1.1)
    multi_json (1.3.6)

Moped::BSON::ObjectId json serialization

Hey there,

Disclaimer: I have very basic knowledge of mongoid (specially it's internals) and ran into this by accident while investigating an issue in my devise-async lib.

This is a very edge case but I thought you'd might like to be aware.

Mongoid 2.4.10 + MultiJson + Yajl engine

u = User.first
MultiJson.encode u.id
# => "[{\"$oid\": \"4fb0e54a07701a25ee000002\"}]"

Mongoid 3.0.0.rc + MultiJson + Yajl engine

u = User.first
MultiJson.encode u.id
# => "[ \"4fb0e54a07701a25ee000002\"]"

The problem seems to come from the fact that BSON::ObjectId from the mongo-ruby-driver implements to_json but Moped::BSON::ObjectId doesn't and so Yajl falls back to to_s.

Not sure if it creates any problem but looks like a potencial backward incompatibility.

If you guys decide to add to_json I'd be happy to help with a patch.

Cheers!

Moped::Query#explain overwrites provided hints

Calling explain on a query containing a hint removes that hint, making it harder to evaluate multiple possible query strategies.

q = User.where(email: "abc").hint(email: 1).query
q.operation.selector
=> {"$query"=>{"email"=>"abc"}, "$hint"=>{"email"=>1}}
q.explain
q.operation.selector
=> {"$query"=>{"email"=>"abc"}, "$orderby"=>{}, "$explain"=>true, "$limit"=>-1}

IMO, the hint should be kept.

from_time: generate a uniq ObjectId

Hi,

the offical bson gem has from_time method to generate a unique object id

BSON::ObjectId.from_time(time, unique: true)

the moped Moped::BSON::ObjectId.from_time only have a time param, and can only generate One unique object id in a second.
I think there should have a way to generate a uniq object id with the Generater's counter field

Using Moped as I18n backend

Hi, first I wanted to thank you for Mongoid and Moped gems.
I am using it on the most Rails projects. Really Wonderfull work!!!
I am thinking about using MongoDB as an I18n backend.
Am I going the right way, or should I use db like Redis?

If I am getting it right, with Redis I don't need to do caching.
Do I need cache translations using MongoDB as backend?
I found an old implementation of key-value store for mongo:
https://github.com/aulizko/mongo-i18n.
So I could create an alternative for Moped, or use already finished backend for redis: https://github.com/jodosha/redis-store/blob/master/redis-i18n/lib/i18n/backend/redis.rb.
What do you think?

http://railscasts.com/episodes/256-i18n-backends

Errno::EHOSTUNREACH not catched?

Hi there,

it seems that #handle_socket_errors does not catch Errno::EHOSTUNREACH, which should be catched explicitly to make things work.

Errno::EHOSTUNREACH: No route to host - connect(2)
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:173:in `initialize'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:173:in `block in initialize'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:205:in `handle_socket_errors'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:173:in `initialize'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:230:in `new'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:230:in `block in connect'
    from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:229:in `connect'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/connection.rb:35:in `connect'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:496:in `connect'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:122:in `ensure_connected'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:516:in `block in flush'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:531:in `logging'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:515:in `flush'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:504:in `process'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:70:in `command'
... 8 levels...
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/cursor.rb:25:in `each'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/query.rb:77:in `each'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/query.rb:77:in `each'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:134:in `block in each'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:633:in `selecting'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual/mongo.rb:133:in `each'
    from /home/metrigo/sites/adserver/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/contextual.rb:18:in `each'

Encoding::CompatibilityError when both key and value are utf-8 chars

throw exception

 {"µ"=>"å"}.__bson_dump__
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
    from /Users/yangyi/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.0.0.rc/lib/moped/bson/extensions/string.rb:29:in `__bson_dump__'
    from /Users/yangyi/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:37:in `block in __bson_dump__'
    from /Users/yangyi/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `each'
    from /Users/yangyi/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.0.0.rc/lib/moped/bson/extensions/hash.rb:36:in `__bson_dump__'
    from (irb):135
    from /Users/yangyi/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

working well (value is utf-8 string)

{"a"=>"å"}.__bson_dump__
 => "\x0F\x00\x00\x00\x02a\x00\x03\x00\x00\x00\xC3\xA5\x00\x00"

working well (key is utf-8 string)

{"µ"=>"a"}.__bson_dump__
 => "\u000E\u0000\u0000\u0000\u0002µ\u0000\u0002\u0000\u0000\u0000a\u0000\u0000"

Aliased field names with moped

I have a Car document which has a field with a symbol of :b, aliased as :year.

class Car 
  include Mongoid::Document

  field :a, :as => :name, :type => String
  field :b, :as => :year, :type => Integer

  belongs_to              :manufacturer
  has_and_belongs_to_many :customers

  validates :name,         :presence => true
  validates :year,         :presence => true
  validates :manufacturer, :presence => true, :uniqueness => { :scope => [:name,:year,:manufacturer] }
end

Using aliased field name with distinct does not work:

# Does not work - returns an empty array
manufacturer.cars.distinct(:year)

Using non-aliased field name works:

# It's all good!
manufacturer.cars.distinct(:b)

I was't sure if this issue should have been placed here or in the mongoid/mongoid project.

Thanks!

nil connection on passenger restart

I recently upgraded from mongoid 2.4 to mongoid 3 and now whenever I deploy to my production server (passenger on engineyard) moped seems to lose its connection to my mongodb. Every page that hits mongodb fails with errors like:

ArgumentError (negative length -36 given):

Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
@Length=113
@request_id=264
@response_to=0
@op_code=2004
@flags=[]
@full_collection_name="db_name_production.$cmd"
@Skip=0
@limit=-1
@selector={:count=>:businesses, :query=>{"name"=>nil}}
@fields=nil>
failed with error nil):

NoMethodError (undefined method `write' for nil:NilClass):

Even restarting passenger doesn't help. Has anyone else experienced this? Any ideas? Thanks!

cluster#with_secondary and Errors::AuthenticationFailure error

When using a replica set with 3 nodes (one arbiter), and a secondary is taken down, instead of the read going to the master, it is failing with an AuthenticationFailure exception.

Looking at the method with_secondary - it appears that the AuthenticationFailure either should be raised as a ConnectionFailure or the method should also trap the AuthenticationFailure and retry on the next node.

The exception is preventing the code from looping around available nodes and choosing the next working mongodb instance.

Moped::Errors::AuthenticationFailure: The operation: #1, :user=>"xyz", :nonce=>"cbd5a5930516cb55", :key=>"412f60e62692d1c7c08d71167ecb7443"}
  @fields=nil>
failed with error "auth fails"
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/node.rb:414:in `login'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/node.rb:47:in `block in apply_auth'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/node.rb:46:in `each'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/node.rb:46:in `apply_auth'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/cluster.rb:176:in `with_secondary'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/session/context.rb:92:in `with_node'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/session/context.rb:31:in `query'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/moped-1.0.0.rc/lib/moped/query.rb:108:in `first'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.0.rc/lib/mongoid/contextual/mongo.rb:196:in `first'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.0.rc/lib/mongoid/contextual.rb:18:in `first'
    from /var/apps/xyz/shared/bundle/ruby/1.9.1/gems/mongoid-3.0.0.rc/lib/mongoid/finders.rb:121:in `first'

ConnectionFailure when trying to connect to a MongoDB v2.1.1 instance

Moped throws the following error when trying to connect to an edge version of MongoDB (v2.1.1).

Moped::Errors::ConnectionFailure: Could not connect to any secondary or primary nodes for replica set #
<Moped::Cluster:0x007fa0f45a72a0 @options={:down_interval=>30, :refresh_interval=>300}, @seeds=["127.0.0.1:27017"],
@nodes=[#<Moped::Node:0x007fa0f45a6f58 @address="127.0.0.1:27017", @ip_address="127.0.0.1", @port=27017, 
@resolved_address="127.0.0.1:27017", @timeout=5, @down_at=2012-06-12 14:00:43 +0100, @refreshed_at=nil, @primary=nil,
@secondary=nil, @connection=#<Moped::Connection:0x007fa0f1634360 @sock=nil, @request_id=0>, @auth={}>]>

undefined method __bson_dump__

I'm getting issues with saving a Mongoid object through the moped driver.

My model

class Task
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title, type: String
end

After I create a new session, and try to insert a document into the collection:

session = Moped::Session.new([ "127.0.0.1:27017" ])
session.use "echo_test"
session[:task].insert(Task.new)

I get the following stack trace....

NoMethodError: undefined method `__bson_dump__' for #<Task:0x00000102ca6ba8>
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/mongoid-3.0.2/lib/mongoid/attributes.rb:225:in `method_missing'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/bson/document.rb:11:in `serialize'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/protocol/message.rb:129:in `block in serialize_documents'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/protocol/message.rb:128:in `each'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/protocol/message.rb:128:in `serialize_documents'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/protocol/message.rb:287:in `serialize'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/connection.rb:137:in `block in write'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/connection.rb:135:in `each'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/connection.rb:135:in `write'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:518:in `block (2 levels) in flush'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:113:in `ensure_connected'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:517:in `block in flush'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:532:in `logging'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:516:in `flush'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:505:in `process'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:228:in `insert'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/session/context.rb:63:in `block in insert'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/session/context.rb:109:in `block in with_node'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/cluster.rb:139:in `block in with_primary'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/node.rb:158:in `ensure_primary'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/cluster.rb:138:in `with_primary'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/session/context.rb:108:in `with_node'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/session/context.rb:56:in `insert'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/collection.rb:88:in `block in insert'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/session.rb:264:in `with'
    from /Users/.rvm/gems/ruby-1.9.3-p194@list/gems/moped-1.1.5/lib/moped/collection.rb:87:in `insert'

I tailed through some of the closed issues and I can confirm that the object id is an Moped::BSON::ObjectId.

I'm using MongoDB (2.0.7), mongoid (3.0.2), moped (1.1.5)

Could you shed some light?

Filter Parameters in Logging

From mongoid/mongoid #2172

It would be neat if the Mongoid logger would filter passwords etc. from its output. Although many may not have logging enabled in production, we find it very useful and still do. However, passwords may then be shown in plain-text in those logs, unless other precautions are taken.

This snippet could be helpful:

filters = Rails.application.config.filter_parameters
f = ActionDispatch::Http::ParameterFilter.new filters
f.filter :password => 'haha' # => {:password=>"[FILTERED]"}

dots in db names go boom

require 'rubygems'

dependencies =
  {
    'mongoid' => [ 'mongoid' , '~> 3.1.0' ] ,
    'moped'   => [ 'moped'   , '~> 1.1.5' ] ,
    'origin'  => [ 'origin'  , '~> 1.0.4' ] ,
  }

dependencies.each do |lib, dependency|
  gem(*dependency)
  require(lib)
end

Mongoid.configure do |config|
  config.connect_to('this-name-goes-boom.development')
end

class A
  include Mongoid::Document
end

p A.count

=begin

/Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:74:in `block in command': The operation: #<Moped::Protocol::Command (Moped::Errors::OperationFailure)
  @length=96
  @request_id=2
  @response_to=0
  @op_code=2004
  @flags=[:slave_ok]
  @full_collection_name="this-name-goes-boom.development.$cmd"
  @skip=0
  @limit=-1
  @selector={:count=>:as, :query=>{}}
  @fields=nil>
failed with error 13530: "bad or malformed command request?"

See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:522:in `[]'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:522:in `block (3 levels) in flush'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:521:in `map'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:521:in `block (2 levels) in flush'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:113:in `ensure_connected'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:517:in `block in flush'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:532:in `logging'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:516:in `flush'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:505:in `process'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/node.rb:70:in `command'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/session/context.rb:51:in `block in command'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/session/context.rb:105:in `block in with_node'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/cluster.rb:180:in `with_secondary'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/session/context.rb:104:in `with_node'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/session/context.rb:50:in `command'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/database.rb:76:in `command'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/moped-1.1.5/lib/moped/query.rb:36:in `count'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/mongoid-3.1.0/lib/mongoid/contextual/mongo.rb:69:in `count'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/mongoid-3.1.0/lib/mongoid/contextual.rb:18:in `count'
        from /Users/ahoward/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/mongoid-3.1.0/lib/mongoid/finders.rb:22:in `count'
        from a.rb:24:in `<main>'


=end

undefined method `[]' for nil:NilClass in node.rb

Hi,

We've started seeing this error since upgrading to Moped 1.1.0/1.1.1:

/path/gems/moped-1.1.1/lib/moped/node.rb:76:in block in command': undefined method[]' for nil:NilClass (NoMethodError)

This is happening in Node#command; it looks like the operation (below) is returning a response (also below) with an empty documents array. This could be because of a problem in the way our development MongoDBs (running 2.0.3) are set up, or how we're using Moped, but there's nothing immediately pointing us in the right direction.

This is almost certainly a Moped issue, since mongoid/mongoid@1a0edad works, whereas mongoid/mongoid@8d900e2 fails, the only difference being the required version of Mongoid.

Any thoughts would be greatly appreciated!

def command(database, cmd, options = {})
  operation = Protocol::Command.new(database, cmd, options)

  process(operation) do |reply|
    result = reply.documents[0]
    raise Errors::OperationFailure.new(
      operation, result
    ) if result["ok"] != 1 || result["err"] || result["errmsg"]
    result
  end
end
operation: #<Moped::Protocol::Command
  @length=58
  @request_id=1
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="admin.$cmd"
  @skip=0
  @limit=-1
  @selector={:ismaster=>1}
  @fields=nil>

reply: #<Moped::Protocol::Reply
  @length=87
  @request_id=4295009167
  @response_to=1
  @op_code=0
  @flags=[]
  @cursor_id=0
  @offset=0
  @count=0
  @documents=[]>

Backtrace:

/Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:76:in block in command': undefined method[]' for nil:NilClass (NoMethodError)
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:524:in []' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:524:inblock (3 levels) in flush'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:523:in map' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:523:inblock (2 levels) in flush'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:115:in ensure_connected' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:519:inblock in flush'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:534:in logging' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:518:inflush'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:507:in process' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:70:incommand'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/node.rb:358:in refresh' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/cluster.rb:101:inblock in refresh'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/cluster.rb:114:in each' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/cluster.rb:114:inrefresh'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/cluster.rb:67:in nodes' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/cluster.rb:176:inwith_secondary'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/session/context.rb:104:in with_node' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/session/context.rb:43:inquery'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/moped-1.1.1/lib/moped/query.rb:108:in first' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/contextual/mongo.rb:199:infirst'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/contextual.rb:18:in `first'

from [application code that runs "where(key: value).limit(1).first"]

from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:440:in _run__2280311454037117474__validation__668028255459460120__callbacks' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in__run_callback'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in _run_validation_callbacks' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:inrun_callbacks'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/callbacks.rb:98:in run_callbacks' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activemodel-3.2.6/lib/active_model/validations/callbacks.rb:53:inrun_validations!'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activemodel-3.2.6/lib/active_model/validations.rb:193:in valid?' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/validations.rb:77:invalid?'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activemodel-3.2.6/lib/active_model/validations.rb:201:in invalid?' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/persistence/insertion.rb:22:inprepare'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/persistence/operations/insert.rb:26:in persist' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/persistence.rb:50:ininsert'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/persistence.rb:79:in save' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/mongoid-21d194b91d33/lib/mongoid/persistence.rb:95:insave!'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/configuration.rb:18:in block in initialize' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/evaluation.rb:15:in[]'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/evaluation.rb:15:in create' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/strategy/create.rb:12:inblock in result'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/strategy/create.rb:9:in tap' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/strategy/create.rb:9:inresult'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/factory.rb:42:in run' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/factory_runner.rb:23:inblock in run'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/notifications.rb:125:in instrument' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/factory_runner.rb:22:inrun'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/factory_girl-3.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in block in define_singular_strategy_method' from /Users/USER/6W/wunderkit/spec/models/stream_spec.rb:23:inblock (2 levels) in <top (required)>'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:201:in module_eval' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:201:insubclass'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:187:in describe' from /Users/USER/6W/wunderkit/spec/models/stream_spec.rb:22:inblock in <top (required)>'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:201:in module_eval' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:201:insubclass'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:187:in describe' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/dsl.rb:18:indescribe'
from [spec file]:in <top (required)>' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:inload'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in block in load' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:inload_dependency'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in load' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:inblock in load_spec_files'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in map' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:inload_spec_files'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in run' from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:inrun'
from /Users/USER/.rvm/gems/ruby-1.9.2-p290@project/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.